JavaScript canvas实现刮刮乐案例 本文实例为大家分享了JavaScript canvas实现刮刮乐效果的具体代码,供大家参考,具体内容如下 效果图 HTML代码: div class=ggk span id=span200元/span canvas id=canvas/canvas /div css代码: .ggk { width: 200px; height: ...
本文实例为大家分享了JavaScript canvas实现刮刮乐效果的具体代码,供大家参考,具体内容如下
效果图
HTML代码:
<div class="ggk">
<span id="span">200元</span>
<canvas id="canvas"></canvas>
</div>
css代码:
.ggk {
width: 200px;
height: 100px;
border: 1px solid #000;
margin: 20px auto;
color: red;
position: relative;
}
.ggk span {
position: absolute;
width: 100%;
height: 100%;
text-align: center;
font-size: 50px;
line-height: 100px;
}
#canvas {
position: absolute;
left: 0;
top: 0;
}
js代码:
var canvas = document.getElementById("canvas")
init()
function init() {
canvas.width = 200;
canvas.height = 100;
var ctx = canvas.getContext("2d")
// 覆盖一层灰色
ctx.save();
ctx.fillStyle = 'rgb(100,100,100)'
ctx.fillRect(0, 0, 200, 100)
draw(ctx)
pro()
}
// 随机内容
function pro() {
var span = document.getElementById("span")
var arr = ["100元", '谢谢惠顾', '200元', '谢谢惠顾', '谢谢惠顾', '谢谢惠顾', '500万', '谢谢惠顾']
var num = Math.floor(Math.random() * (arr.length - 1))
var text = arr[num]
span.innerHTML = text
}
function draw(ctx){
// 点下事件
canvas.onmousedown = function(e){
// 移动事件
var downX= e.offsetX
var downY= e.offsetY
ctx.beginPath()
// ctx.globalCompositeOperation = 'destination-out'
ctx.lineWidth = 10;
ctx.moveTo(downX,downY)
canvas.onmousemove = function(e){
var x = e.offsetX
var y = e.offsetY
// ctx.lineTo(x,y)
ctx.clearRect(x,y,20,20)
ctx.stroke()
}
}
// 鼠标弹起事件
canvas.onmouseup = function(){
canvas.onmousemove = null
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。
沃梦达教程
本文标题为:JavaScript canvas实现刮刮乐案例


基础教程推荐
猜你喜欢
- WatchKit 支持 html 吗?有没有像 UIWebview 这样的控制器? 2022-01-01
- jQuery File Upload - 如何识别所有文件何时上传 2022-01-01
- Node.js 有没有好的索引/搜索引擎? 2022-01-01
- 如何使用 CSS 显示和隐藏 div? 2022-01-01
- 每次设置弹出窗口的焦点 2022-01-01
- 什么是不使用 jQuery 的经验技术原因? 2022-01-01
- 为什么我在 Vue.js 中得到 ERR_CONNECTION_TIMED_OUT? 2022-01-01
- Javascript 在多个元素上单击事件侦听器并获取目标 2022-01-01
- 如何使用sencha Touch2在单页中显示列表和其他标签 2022-01-01
- 如何在特定日期之前获取消息? 2022-01-01