html:canvas画布绘图简单入门-刮刮乐-4
【摘要】
canvas示例系列:
html:canvas画布绘图简单入门-1html:canvas画布绘图简单入门-2html:canvas画布绘图简单入门-绘制时钟-3html:canvas画布绘图简单入门-刮...
canvas示例系列:
在线示例:https://mouday.github.io/front-end-demo/canvas/canvas-scrape.html
<style>
body {
margin: 0;
padding: 0;
height: 100vh;
display: flex;
align-items: center;
justify-content: center;
}
.box-wrap {
position: relative;
width: 300px;
height: 150px;
border: 1px solid #eeeeee;
}
.box {
position: absolute;
width: 100%;
height: 100%;
display: flex;
align-items: center;
justify-content: center;
font-size: 25px;
color: red;
letter-spacing: 20px;
}
.canvas {
position: absolute;
width: 100%;
height: 100%;
}
</style>
<!-- 刮刮乐 -->
<div class="box-wrap">
<div class="box"
id="text">谢谢惠顾</div>
<canvas class="canvas"
id="canvas"
width="600"
height="300"></canvas>
</div>
<script>
let canvas = document.querySelector('#canvas');
let text = document.querySelector('#text');
let ctx = canvas.getContext('2d');
let clientWidth = canvas.clientWidth;
let clientHeight = canvas.clientHeight;
let width = canvas.width
let height = canvas.height
// 缩放比例
let widthScale = width / clientWidth;
let heightScale = height / clientHeight;
// 绘制背景图
ctx.fillStyle = 'gray';
ctx.fillRect(0, 0, width, height);
ctx.save();
// 绘制文字
ctx.translate(width / 2, height / 2);
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.font = "50px 微软雅黑"
ctx.fillStyle = 'white';
ctx.fillText('刮开查看', 0, 0);
// ctx.fill();
ctx.restore();
// 监听鼠标事件
let isDraw = false;
canvas.onmousedown = function (e) {
// console.log(e);
isDraw = true;
}
canvas.onmouseup = function (e) {
// console.log(e);
isDraw = false;
}
canvas.onmousemove = function (e) {
if (!isDraw) {
return;
}
// console.log(e);
// ctx.fillStyle = 'white';
// 在源图像之外显示目标图像。只有源图像之外的目标图像部分会被显示,源图像是透明的
ctx.globalCompositeOperation = 'destination-out'
// console.log(e.offsetX, e.offsetY);
ctx.arc(e.offsetX * widthScale, e.offsetY * heightScale, 30, 0, Math.PI / 180 * 360);
ctx.fill();
}
// 奖品设置
let list = [{
title: '一等奖',
probability: 0.1
},
{
title: '二等奖',
probability: 0.2
},
{
title: '三等奖',
probability: 0.3
}
]
let randomNum = Math.random();
console.log(randomNum);
let randomNumUpperBound = 0;
for (let item of list) {
randomNumUpperBound += item.probability
if (randomNum < randomNumUpperBound) {
text.innerText = item.title;
break;
}
}
</script>
- 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
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
文章来源: pengshiyu.blog.csdn.net,作者:彭世瑜,版权归原作者所有,如需转载,请联系作者。
原文链接:pengshiyu.blog.csdn.net/article/details/122266509
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)