JavaScript canvas to achieve scratch lottery example

JavaScript canvas to achieve scratch lottery example

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:
  • HTML+CSS+JavaScript to make a girlfriend version of scratch card (you will learn it once you see it)
  • JavaScript to achieve scratch effect
  • Native JavaScript to implement scratch tickets
  • 20 lines of JS code to achieve the effect of web scratch lottery
  • How to implement scratch cards on mobile devices (js+HTML5)
  • js HTML5 mobile scratch code

<<:  Solve the problem of docker images disappearing

>>:  Introduction and use of triggers and cursors in MySQL

Recommend

How to get the height of MySQL innodb B+tree

Preface The reason why MySQL's innodb engine ...

js realizes 3D sound effects through audioContext

This article shares the specific code of js to ac...

A brief introduction to MySQL functions

Table of contents 1. Mathematical functions 2. St...

Docker file storage path, get container startup command operation

The container has already been created, how to kn...

Solution to the Docker container cannot be stopped and deleted

Find the running container id docker ps Find the ...

Example of how to generate random numbers and concatenate strings in MySQL

This article uses an example to describe how MySQ...

Installation method of mysql-8.0.17-winx64 under windows 10

1. Download from the official website and unzip h...

Tomcat uses Log4j to output catalina.out log

Tomcat's default log uses java.util.logging, ...

How to write configuration files and use MyBatis simply

How to write configuration files and use MyBatis ...

Super detailed tutorial to implement Vue bottom navigation bar TabBar

Table of contents Project Introduction: Project D...

Discussion on more reasonable creation rules for MySQL string indexes

Preface Regarding the use of MySQL indexes, we ha...

How to deeply understand React's ref attribute

Table of contents Overview 1. Creation of Refs ob...

Prototype and prototype chain prototype and proto details

Table of contents 1. Prototype 2. Prototype chain...