canvas示例系列:
- html:canvas画布绘图简单入门-1
- html:canvas画布绘图简单入门-2
- html:canvas画布绘图简单入门-绘制时钟-3
- html:canvas画布绘图简单入门-刮刮乐-4
在线示例:https://mouday.github.io/front-end-demo/canvas/canvas-scrape.html
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%;
}
谢谢惠顾
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
关注
打赏
最近更新
- 深拷贝和浅拷贝的区别(重点)
- 【Vue】走进Vue框架世界
- 【云服务器】项目部署—搭建网站—vue电商后台管理系统
- 【React介绍】 一文带你深入React
- 【React】React组件实例的三大属性之state,props,refs(你学废了吗)
- 【脚手架VueCLI】从零开始,创建一个VUE项目
- 【React】深入理解React组件生命周期----图文详解(含代码)
- 【React】DOM的Diffing算法是什么?以及DOM中key的作用----经典面试题
- 【React】1_使用React脚手架创建项目步骤--------详解(含项目结构说明)
- 【React】2_如何使用react脚手架写一个简单的页面?