本文实例为大家分享了Vue使用鼠标在Canvas上绘制矩形的具体代码,供大家参考,具体内容如下
1.代码
<template> <div class="test" style="background-color: burlywood;"> <canvas id="myCanvas" ref="myCanvas" width="460" height="240" @mousedown="mousedown" @mouseup="mouseup" @mousemove="mousemove"> </canvas> </div> </template> <script> export default { name:"hello-world", data() { return { flag: false, x: 0, y: 0 }; }, watch: {}, computed: {}, methods: { mousedown(e){ console.log("鼠标落下"); this.flag = true; this.x = e.offsetX; // 鼠标落下时的X this.y = e.offsetY; // 鼠标落下时的Y }, mouseup(e){ console.log("鼠标抬起"); this.flag = false; }, mousemove(e){ console.log("鼠标移动"); this.drawRect(e); }, drawRect(e){ if(this.flag){ console.log("绘制图形"); const canvas = this.$refs.myCanvas; var ctx = canvas.getContext("2d"); let x = this.x; let y = this.y; ctx.clearRect(0,0,canvas.width,canvas.height); ctx.beginPath(); //设置线条颜色,必须放在绘制之前 ctx.strokeStyle = '#00ff00'; // 线宽设置,必须放在绘制之前 ctx.lineWidth = 1; ctx.strokeRect(x,y,e.offsetX-x,e.offsetY-y); } } }, created() { }, mounted() { } }; </script> <style scoped> #myCanvas{ background-color: forestgreen; background-image:url('../bg.jpg'); } </style>
2.运行截图
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持站长技术。
IT之家2月18日消息外媒 Windows Latest 报道,微软正在与谷歌合作进行一项新的改...
文章目录 前言 本周最重要的五件事情 本周搞砸的四件事情 本周的四个启发 前言 ...
2 月 18 日消息 据外媒 Windowslatest 报道,在预览版本中发现的参考资料表明,...
1.ajax跨域传递值是所需要的回传的类型为jsonp $.ajax({url: "http://.......",t...
在项目开始之前我们可以先去了解一下IConfiguration接口,.Net Core Web应用程序...
一.前言 .NET Core 是一个通用开发平台,由 Microsoft 和 GitHub 上的 .NET 社区...
JSP spring boot / cloud 使用filter防止XSS 一.前言 XSS(跨站脚本攻击) 跨站脚...
首先插件配备好了,写一个HTML测试一下 首先创建一个文件夹,创建一个HTML 文件...
昨天刚学了html的一些内容,就迫不及待的想做个京东上面的搜索条,结果做是做出...
第一课趣味二进制——修改植物大战僵尸数据 任务介绍 学习目标 知识需求 需求工...