码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • canvas绘制马路和屏幕


     

    1. <script>
    2. export default {
    3. data () {
    4. return {
    5. road: {
    6. width: 550,
    7. height: 200,
    8. color: "gray"
    9. },
    10. screens: [
    11. { x: 0, y: 0, width: 100, height: 40, text: "屏幕 1: 文字 1" },
    12. { x: 150, y: 0, width: 100, height: 40, text: "屏幕 2: 文字 2" },
    13. { x: 300, y: 0, width: 100, height: 40, text: "屏幕 3: 文字 3" },
    14. { x: 450, y: 0, width: 100, height: 40, text: "屏幕 4: 文字 4" }
    15. ],
    16. textSpeed: 20 // 文字滚动速度
    17. }
    18. },
    19. mounted() {
    20. this.updateScreen();
    21. },
    22. methods: {
    23. updateScreen() {
    24. const canvas = this.$refs.roadCanvas;
    25. const ctx = canvas.getContext("2d");
    26. // 渲染道路
    27. ctx.fillStyle = this.road.color;
    28. ctx.fillRect(0, 10, this.road.width, this.road.height);
    29. // 绘制白色斑马线
    30. ctx.fillStyle = "white";
    31. const lineWidth = 80; // 斑马线宽度
    32. const lineHeight = 10; // 斑马线高度
    33. const gap = 40; // 斑马线间隔
    34. let x = gap;
    35. while (x < canvas.width) {
    36. ctx.fillRect(x, 60, lineWidth, lineHeight);
    37. ctx.fillRect(x, 80, lineWidth, lineHeight);
    38. ctx.fillRect(x, 100, lineWidth, lineHeight);
    39. x += lineWidth + gap * 2;
    40. }
    41. this.screens.forEach(screen => {
    42. ctx.fillStyle = "black";
    43. ctx.fillRect(screen.x, screen.y, screen.width, screen.height);
    44. // 在屏幕上绘制滚动文字
    45. ctx.fillStyle = "white";
    46. ctx.font = "16px Arial";
    47. // 计算滚动位置
    48. const scrollPosition = (Date.now() * this.textSpeed / 1000) % (screen.text.length * 16);
    49. // console.log('123',scrollPosition)
    50. // 计算截断的位置,确保文字不会溢出屏幕
    51. const truncatePosition = Math.min(scrollPosition, screen.width);
    52. // 绘制滚动文字,通过截断位置来实现隐藏溢出部分
    53. ctx.fillText(
    54. screen.text.slice(0, truncatePosition / 10),
    55. screen.x + screen.width - truncatePosition,
    56. screen.y + 30
    57. );
    58. });
    59. requestAnimationFrame(this.updateScreen);
    60. },
    61. }
    62. };
    63. script>
    64. <style>
    65. /* Add any custom styles here */
    66. style>

  • 相关阅读:
    java高校教师个人信息管理系统计算机毕业设计MyBatis+系统+LW文档+源码+调试部署
    程序员从网上抄代码,被老板发现后....
    【MySQL从入门到精通】【高级篇】(十九)索引的分类&创建索引的三种方式&删除索引的两种方式
    MeterSphere 至学篇
    游戏中的随机——“动态平衡概率”算法
    微服务追踪SQL(支持Isto管控下的gorm查询追踪)
    SqlServer函数,存储过程的创建和使用
    盘点30个Python树莓派源码Python爱好者不容错过
    端口扫描工具是什么?端口扫描工具有什么用
    随笔,最近的生活
  • 原文地址:https://blog.csdn.net/weixin_51258044/article/details/133743812
  • 最新文章
  • 沪漂五周年了:我越来越迷茫了
    Agentic Skill Routing 实战:别再把所有 Skill 塞进 AI Agent 上下文
    MySQL-Seconds_behind_master的精度误差
    [MAF预定义ChatClient中间件-03]CachingChatClient——利用缓存省钱省时间
    AI的至暗历史:从万众期待到被政府撤资,AI的两次死亡徘徊
    Agent OS :五种驯服不确定性的范式
    PortSwigger SQL注入LAB11
    数据库即时编译JIT
    [Begin]AI Learn Data Day 0
    深度学习进阶(二十七)现代 LLM 的核心架构设计其二:SwiGLU
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
小工具 小游戏
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1

京公网安备 11010502049817号