This article shares the specific code of JavaScript canvas to achieve the scratch effect for your reference. The specific content is as follows Rendering HTML code: <div class="ggk"> <span id="span">200 yuan</span> <canvas id="canvas"></canvas> </div> CSS code: .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 code: var canvas = document.getElementById("canvas") init() function init() { canvas.width = 200; canvas.height = 100; var ctx = canvas.getContext("2d") // Cover with a layer of gray ctx.save(); ctx.fillStyle = 'rgb(100,100,100)' ctx.fillRect(0, 0, 200, 100) draw(ctx) pro() } // Random content function pro() { var span = document.getElementById("span") var arr = ["100 yuan", 'Thank you for your patronage', '200 yuan', 'Thank you for your patronage', 'Thank you for your patronage', 'Thank you for your patronage', '5 million', 'Thank you for your patronage'] var num = Math.floor(Math.random() * (arr.length - 1)) var text = arr[num] span.innerHTML = text } function draw(ctx){ //Click event canvas.onmousedown = function(e){ //Move event 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() } } // Mouse pop-up event canvas.onmouseup = function(){ canvas.onmousemove = null } } The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM. You may also be interested in:
|
<<: Solve the problem of docker images disappearing
>>: Introduction and use of triggers and cursors in MySQL
The problem that MYSQL5.7.17 cannot connect under...
When you learn MySQL, you will find that it comes...
Today I will share with you a source code contain...
Problem Description I want to achieve the followi...
KILL [CONNECTION | QUERY] processlist_id In MySQL...
Table of contents Set is a special collection who...
Introduction: The configuration of Docker running...
Prerequisite: The web developer plugin has been in...
Date type differences and uses MySQL has five dat...
Generally speaking, in order to get more complete...
Table of contents Preface 1. What is selenium? 2....
Author: Guan Changlong is a DBA in the Delivery S...
<html> <head> <meta http-equiv=&quo...
Add an input file HTML control to the web page: &...
This article mainly focuses on the installation a...