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

Problems and solutions for MYSQL5.7.17 connection failure under MAC

The problem that MYSQL5.7.17 cannot connect under...

The latest popular script Autojs source code sharing

Today I will share with you a source code contain...

MySQL kill command usage guide

KILL [CONNECTION | QUERY] processlist_id In MySQL...

In-depth explanation of Set and WeakSet collections in ES6

Table of contents Set is a special collection who...

Implementation of running springboot project with Docker

Introduction: The configuration of Docker running...

How to disable web page styles using Firefox's web developer

Prerequisite: The web developer plugin has been in...

How to add default time to a field in MySQL

Date type differences and uses MySQL has five dat...

Summary of four situations of joint query between two tables in Mysql

Generally speaking, in order to get more complete...

Introduction to deploying selenium crawler program under Linux system

Table of contents Preface 1. What is selenium? 2....

MySQL 8.0.23 Major Updates (New Features)

Author: Guan Changlong is a DBA in the Delivery S...

HTML input file control limits the type of uploaded files

Add an input file HTML control to the web page: &...