码农知识堂 - 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>

  • 相关阅读:
    drf之day04: drf请求与响应,drf能够解析的请求编码,响应编码,drf之视图组件,2个视图基类
    C++求欧拉角(eigen库中暴露的一些问题)
    Vue.js源码解析:Vue.js 常用工具函数 util.js
    Git 笔记
    跨模态检索论文阅读:(PTP)Position-guided Text Prompt for Vision-Language Pre-training
    大语言模型LLM分布式训练:TensorFlow下的大语言模型训练实践(LLM系列05)
    【数据库编码集】oracle 10g数据库查询中文因编码格式不同,导致显示乱码。客户端转码解决办法,mybatis全局TypeHandler,mp多数据源
    JSD-2204-(续)SpringGateway网关-Elasticsearch-Day06
    OpenCV 中Mat.depth()的理解——每个像素的位数——每个像素中每个通道的精度
    电话订货、线下赊账、人工打单,批发生意越做越简单
  • 原文地址:https://blog.csdn.net/weixin_51258044/article/details/133743812
  • 最新文章
  • 【JVM】编译执行与解释执行的区别是什么?JVM 使用哪种方式?
    用 Hashids 优雅解决 C 端自增 ID 暴露问题
    V8引擎 精品漫游指南--Ignition篇(上) 指令 栈帧 槽位 调用约定 内存布局 基础内容
    LLVM Pass快速入门(四):代码插桩
    milkup:桌面端 markdown AI续写和即时渲染
    基于项目工程构建SBOM(软件物料清单)的研究
    鸿蒙应用开发UI基础第二节:鸿蒙应用程序框架核心解析与实操
    .NET 中如何快速实现 List 集合去重?
    扣子Coze实战:从0到1打造抖音+小红书热点监控智能体
    浅谈数据访问层
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
小工具 小游戏
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1

京公网安备 11010502049817号