1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
| <template> <div class="guaguale"> <p>一等奖</p> <canvas id="canvas" width="500" height="1000" @mousedown="down" @mouseup="up" @mousemove="move" > 您的浏览器不支持canvas! </canvas> </div> </template> <script> export default { data() { return { flag:false, lx:'', ly:'' } }, mounted() { this.guagua(); }, methods:{ guagua() { let cv = document.getElementById("canvas"), p = document.getElementsByTagName("p")[0]; //设置中奖几率 let num = 10000 * Math.random(), result = ""; if (num < 1000) { // result = "三等奖"; } if (num < 50) { // result = "二等奖"; } if (num < 5) { // result = "一等奖"; } p.innerText = result; try { let ct = cv.getContext("2d"); //绘制背景色 ct.fillStyle = "skyblue"; ct.fillRect(0, 0, 500, 1000); //设置绘制状态 ct.lineCap = "round"; //设置线条两端为圆弧 ct.lineJoin = "round"; //设置线条转折为圆弧 ct.lineWidth = 60; /*设置新绘制清除新绘制内容和原内容的重叠区域*/ ct.globalCompositeOperation = "destination-out"; } catch (e) { console.log(e); } }, down(e) { this.flag = true; this.lx = e.offsetX; this.ly = e.offsetY; }, up() { this.flag = false; }, move(e) { if (this.flag) { let cv = document.getElementById("canvas"); let ct = cv.getContext("2d"); ct.beginPath(); ct.moveTo(this.lx, this.ly); ct.lineTo(e.offsetX, e.offsetY); ct.stroke(); ct.closePath();
//更新坐标 this.lx = e.offsetX; this.ly = e.offsetY; } } } } </script> <style lang="scss" scoped> #canvas { /* canvas 默认宽高 300*150 css设置的宽高,只能设置canvas对象在页面显示的大小。 并不能增加画布内部的像素数 */ border: 1px solid black; /* width: 500px; height: 500px; */ margin: 0 auto; position: absolute; left: 0; top: 0; }
.guaguale { width: 500px; height: 500px; background: url("../../assets/dou.png") no-repeat; background-size: 100% 100%; position: relative; }
.guaguale p { font-size: 50px; line-height: 500px; text-align: center; width: 100%; } </style>
|