Realize super cool water light effect based on canvas

Realize super cool water light effect based on canvas

This article example shares with you the specific code for implementing a super cool water light effect based on canvas for your reference. The specific content is as follows

<!DOCTYPE html>  
<html>  
<head>  
    <meta charset="UTF-8">  
    <title>Super cool water light effect based on canvas</title>  
    <style>
        *{
            margin: 0;
            padding: 0;
        }
        canvas{
            border: 1px solid red;
            width: 100%;
            height: 100%;
        }
    </style>
</head>  
<body onselectstart="return false">  
        <!-- Add the canvas tag and add a red border to make it easier to see on the page-->  
        <canvas id="myCanvas" >  
        Your browser does not support the canvas tag.   
        </canvas>
<script src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
<script type="text/javascript">
    var canvas = document.getElementById("myCanvas");  
    var ctx = canvas.getContext("2d");    
    var cx1 = canvas.offsetLeft;
    var cy1 = canvas.offsetTop;
    var cx2 = canvas.offsetLeft + canvas.offsetWidth;
    var cy2 = canvas.offsetTop + canvas.offsetHeight;
    var bbox = canvas.getBoundingClientRect();  
    $(function(){
        var direction = 'right',x = y = right_count = down_count = left_count = up_count = 0;
        ctx.beginPath(); //Start a new drawing path ctx.moveTo(x, y); //Define the starting point coordinates of the line as (0,0)
        setInterval(function(){
            ctx.strokeStyle = '#'+Math.floor(Math.random()*16777215).toString(16);
            switch(direction){
                case 'right':
                    if(x >= 300 - right_count){
                        direction = 'down';    
                        right_count++;
                    }else{
                        x++;
                    }
                    break;
                case 'down':
                    if(y >= 150 - down_count){
                        direction = 'left';
                        down_count++;
                    }else{
                        y++;
                    }
                    break;
                case 'left':
                    if(x <= left_count){
                        direction = 'up';
                        left_count++;
                    }else{
                        x--;
                    }
                    break;
                case 'up':
                    if(y <= up_count + 1){
                        direction = 'right';
                        up_count++;
                    }else{
                        y--;
                    }
                    break;
            }
            ctx.lineTo(x, y);
            ctx.lineCap = 'round';
            ctx.lineWidth = 1; //Set the width of the line segment ctx.stroke(); //Draw a straight line along the path of the coordinate point sequence }, 1);
    }) 
</script>  
</body>  
</html>

Effect screenshots:

The running lights are always running, and they are mainly circling here.

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:
  • Canvas realizes searchlight effect

<<:  In-depth analysis of the Identifier Case Sensitivity problem in MySQL

>>:  Detailed explanation of two ways of Linux service management: service and systemctl

Recommend

Summary of some common writing methods that cause MySQL index failure

Preface Recently, I have been busy dealing with s...

What are Web Slices?

IE8 new feature Web Slices (Web Slices) Microsoft...

Mysql dynamically updates the database script example explanation

The specific upgrade script is as follows: Dynami...

Summary of MySQL view principles and usage examples

This article summarizes the principles and usage ...

mysql 8.0.12 winx64 download and installation tutorial

MySQL 8.0.12 download and installation tutorial f...

jQuery implements the mouse drag image function

This example uses jQuery to implement a mouse dra...

Detailed graphic and text instructions for installing MySQL 5.7.20 on Mac OS

Installing MySQL 5.7 from TAR.GZ on Mac OS X Comp...

How to design MySQL statistical data tables

Table of contents Is real-time update required? M...

Pure CSS to achieve cool charging animation

Let’s take a look at what kind of charging animat...

translate(-50%,-50%) in CSS achieves horizontal and vertical centering effect

translate(-50%,-50%) attributes: Move it up and l...

Implementation code of jquery step progress axis plug-in

A jQuery plugin every day - step progress axis st...

Install tomcat and deploy the website under Linux (recommended)

Install jdk: Oracle official download https://www...

How to implement responsive layout in vue-cli

When we are doing front-end development, we will ...

React ref usage examples

Table of contents What is ref How to use ref Plac...

JavaScript Basics Series: Functions and Methods

Table of contents 1. The difference between funct...