目录
1.刮刮乐
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>刮刮乐</title>
<style>
#ggk {
width: 600px;
height: 400px;
font-size: 30px;
text-align: center;
line-height: 400px;
position: absolute;
top: 100px;
z-index: -1;
}
</style>
</head>
<body>
<div id="ggk">谢谢惠顾</div>
<canvas id="canvas" width="600px" height="400px"></canvas>
</body>
<script>
// 获取画笔
var c1 = document.getElementById("canvas");
var ctx = c1.getContext("2d");
// 绘制图片
let img = new Image();
img.src = "./img/ggk.jpeg";
img.onload = function () {
ctx.drawImage(img, 0, 0, 600, 400);
};
// 判断是否可以画
var isDraw = false;
// 鼠标按下的时候设置为可以绘画
c1.onmousedown = function () {
isDraw = true;
};
// 鼠标抬起的时候设置为不可以绘画
c1.onmouseup = function () {
isDraw = false;
};
// 当鼠标移动并且可以绘画时,获取鼠标的坐标信息并以此为圆心绘制圆形
c1.onmousemove = function (e) {
if (isDraw) {
var x = e.pageX;
var y = e.pageY;
// destination-out模式可以消除重叠的部分,也就是我们画的圆形和图片重叠的部分,然后显示不重叠的部分
ctx.globalCompositeOperation = "destination-out";
ctx.arc(x, y, 20, 0, 2 * Math.PI);
ctx.fill();
}
};
// 设置随机数,模拟概率,增加趣味性
let random = Math.random();
var ggk = document.querySelector("#ggk");
if (random < 0.1) {
ggk.innerHTML = "恭喜你获得iPhone14";
} else {
ggk.innerHTML = "谢谢惠顾";
}
</script>
</html>
2.在线画板
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<style>
body {
background-color: #87ceeb;
}
#c1 {
background-color: #fff;
}
/* 设置按钮点击时的样式 */
.active {
color: #fff;
background-color: orange;
}
</style>
</head>
<body>
<canvas id="c1" width="800px" height="400px"></canvas>
<hr