This is a small scratch-off Demo processed on the mobile terminal
Directly upload the complete code
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="format-detection" content="telephone=no"> <meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width"> <style type="text/css"> #guaJiang .bjc { color: black; height: 70px; width: 240px; text-align: center; line-height: 70px; font-size: 20px; position: absolute; top: 300px; left: 60px; background-image: url(img/4-01.png); background-repeat: no-repeat; background-size: 18px 18px; background-position: 80% 53% } #guaJiang .guaCanvas { z-index: 3; position: absolute; top: 300px; left: 60px } </style> <title>网红</title> </head> <body > <div id='guaJiang'> <div class='bjc'>萌赚送你1.5</div> <canvas id="myCanvas" class='guaCanvas' width="240" height="70"></canvas> </div> </body> </html>
Then the JS code
var clientWidth = document.documentElement.clientWidth; if(clientWidth *1 >0){ document.querySelector("#guaJiang .bjc").style.left = (clientWidth - 240)/2 + "px"; document.querySelector("#guaJiang .guaCanvas").style.left = (clientWidth - 240)/2 + "px"; } // 得到画笔 var cvs = document.getElementById("myCanvas"), ctx = cvs.getContext("2d"), touchRadius = 10; // 默认手指触摸半径,可以自定义设置 // 默认填充灰色来遮住文字 ctx.fillStyle = "#ccc"; ctx.fillRect(0, 0, 240, 70); // fillRect,用矩形填充 ctx.font = '15px arial'; ctx.fillStyle = 'white'; ctx.fillText('您获得一次刮奖机会', 56,40); // 画园的方法 // @param { integer } 圆心的x坐标 // @param { integer } 圆心的y坐标 // @param { integer } 圆心半径 // @param { string } 填充的颜色(本例中会通过其余代码设置为‘透明’,所以这个设置可以忽略) var fillCircle = function (x, y, radius, fillColor) { this.fillStyle = fillColor || "#eee"; this.beginPath(); this.moveTo(x-90, y-300); this.arc(x-90, y-300, radius, 0, Math.PI * 2, false); // 标准画圆 this.fill(); }; // 得到涂抹的百分比 var getTransparentPercent = function (ctx, width, height) { var imgData = ctx.getImageData(0, 0, width, height), // 得到canvas的像素信息 pixles = imgData.data, transPixs = []; for (var i = 0, j = pixles.length; i < j; i += 4) { // 因为存储的结构为[R, G, B, A],所以要每次跳4个长度 var a = pixles[i + 3]; // 拿到存储alpha通道的值 if (a === 0) { // alpha通道为0,就代表透明 transPixs.push(i); } } return (transPixs.length / (pixles.length / 4) * 100).toFixed(2); } // 需要判断是PC还是手机 var device = /android|webos|iphone|ipad|ipod|blackberry|iemobile|opera mini/i.test(navigator.userAgent.toLowerCase()), clickEvtName = device ? 'touchstart' : 'mousedown', moveEvtName = device ? 'touchmove' : 'mousemove'; // 判断是不是开始触摸等 if (!device) { var isMouseDown = false; document.addEventListener('mouseup', function (e) { isMouseDown = false; }, false); } else { document.addEventListener("touchmove", function (e) { if (isMouseDown) { e.preventDefault(); } }, false); document.addEventListener('touchend', function (e) { isMouseDown = false; }, false); } // 开始移动 cvs.addEventListener(clickEvtName, function (e) { isMouseDown = true; var x = (device ? e.touches[0].clientX : e.clientX); var y = (device ? e.touches[0].clientY : e.clientY); ctx.globalCompositeOperation = 'destination-out'; // 关键部分,描述当在canvas上再次绘画时候的情况,这个设置便是之前所说的透明 fillCircle.call(ctx, x, y, touchRadius); console.log("当前涂抹比例为:" + getTransparentPercent(ctx, 240, 70)); }, false); // 移动中 cvs.addEventListener(moveEvtName, function (e) { if (!device && !isMouseDown) { return false; } var x = (device ? e.touches[0].clientX : e.clientX); var y = (device ? e.touches[0].clientY : e.clientY); ctx.globalCompositeOperation = 'destination-out'; fillCircle.call(ctx, x, y, touchRadius); console.log("当前涂抹比例为:" + getTransparentPercent(ctx, 240, 70)); }, false);
The rendering after running

Python and JavaScript each have their own advantages, and the choice depends on project needs and personal preferences. 1. Python is easy to learn, with concise syntax, suitable for data science and back-end development, but has a slow execution speed. 2. JavaScript is everywhere in front-end development and has strong asynchronous programming capabilities. Node.js makes it suitable for full-stack development, but the syntax may be complex and error-prone.

JavaScriptisnotbuiltonCorC ;it'saninterpretedlanguagethatrunsonenginesoftenwritteninC .1)JavaScriptwasdesignedasalightweight,interpretedlanguageforwebbrowsers.2)EnginesevolvedfromsimpleinterpreterstoJITcompilers,typicallyinC ,improvingperformance.

JavaScript can be used for front-end and back-end development. The front-end enhances the user experience through DOM operations, and the back-end handles server tasks through Node.js. 1. Front-end example: Change the content of the web page text. 2. Backend example: Create a Node.js server.

Choosing Python or JavaScript should be based on career development, learning curve and ecosystem: 1) Career development: Python is suitable for data science and back-end development, while JavaScript is suitable for front-end and full-stack development. 2) Learning curve: Python syntax is concise and suitable for beginners; JavaScript syntax is flexible. 3) Ecosystem: Python has rich scientific computing libraries, and JavaScript has a powerful front-end framework.

The power of the JavaScript framework lies in simplifying development, improving user experience and application performance. When choosing a framework, consider: 1. Project size and complexity, 2. Team experience, 3. Ecosystem and community support.

Introduction I know you may find it strange, what exactly does JavaScript, C and browser have to do? They seem to be unrelated, but in fact, they play a very important role in modern web development. Today we will discuss the close connection between these three. Through this article, you will learn how JavaScript runs in the browser, the role of C in the browser engine, and how they work together to drive rendering and interaction of web pages. We all know the relationship between JavaScript and browser. JavaScript is the core language of front-end development. It runs directly in the browser, making web pages vivid and interesting. Have you ever wondered why JavaScr

Node.js excels at efficient I/O, largely thanks to streams. Streams process data incrementally, avoiding memory overload—ideal for large files, network tasks, and real-time applications. Combining streams with TypeScript's type safety creates a powe

The differences in performance and efficiency between Python and JavaScript are mainly reflected in: 1) As an interpreted language, Python runs slowly but has high development efficiency and is suitable for rapid prototype development; 2) JavaScript is limited to single thread in the browser, but multi-threading and asynchronous I/O can be used to improve performance in Node.js, and both have advantages in actual projects.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Dreamweaver Mac version
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

SublimeText3 Chinese version
Chinese version, very easy to use

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool
