JavaScript canvas implements moving the ball following the mouse

JavaScript canvas implements moving the ball following the mouse

This article example shares the specific code of js following the mouse to move the ball for your reference. The specific content is as follows

<!DOCTYPE html>
<html>
 <head>
 <meta charset="utf-8">
 <title></title>
 <style>
 canvas{
 border: 1px solid #000;
 }
 </style>
 </head>
 <body>
 <canvas id="mycanvas" width="1500" height="800"></canvas>
 <script>
 // Create a canvas var canvas = document.getElementById('mycanvas');
 var ctx = canvas.getContext('2d');
 // Ball class function Ball(x, y) {
 this.x = x;
 this.y = y;
 // Initial radius this.r = parseInt(Math.random() * 50) + 10;
 this.step = parseInt(Math.random() * 5) + 0.1;
 // Set random color this.color = getRandom();
 // Set random direction this.dx = parseInt(Math.random() * 10) - 5;
 this.dy = parseInt(Math.random() * 10) - 5;
 // Load the object into the array ballArr.push(this);
 }
 // Remove the object from the array Ball.prototype.remove = function() {
 for (var i = 0; i < ballArr.length; i++) {
 if (ballArr[i] == this) {
 ballArr.splice(i, 1);
 }
 }
 }
 // Update data Ball.prototype.update = function() {
 // Update data this.x += this.dx;
 this.y += this.dy;
 this.r -= this.step;
 // Clear the balls in the array if (this.r <= 0) {
 this.remove();
 }
 // If it exceeds the boundary, the ball continues to move if (this.x < 0) {
 this.x = 1500;
 this.color = getRandom();
 }
 else if (this.x > 1500) {
 this.x = 0;
 this.color = getRandom();
 }
 else if (this.y < 0) {
 this.y = 800;
 this.color = getRandom();
 }
 else if (this.y > 800) {
 this.y = 0;
 this.color = getRandom();
 }
 }
 // Render the ball Ball.prototype.render = function() {
 ctx.beginPath();
 ctx.arc(this.x, this.y, this.r, 0, Math.PI * 2, false);
 ctx.fillStyle = this.color;
 ctx.fill();
 }
 // canvas DOM2 event canvas.addEventListener("mousemove", function(event) {
 new Ball(event.offsetX, event.offsetY);
 });
 var ballArr = [];
 // Timer for animation rendering and update setInterval(function() {
 // Animation logic // Clear screen - update - render ctx.clearRect(0, 0, canvas.width, canvas.height);
 // Update and render the ball for (var i = 0; i < ballArr.length; i++) {
 ballArr[i].update();
 if (ballArr[i]) {
 ballArr[i].render();
 }
 }
 }, 30);
 // Random color function getRandom() {
 var allType = "0,1,2,3,4,5,6,7,8,9,a,b,c,d,e,f";
 var allTypeArr = allType.split(",");
 var color = "#";
 // concatenate color strings for (var i = 0; i < 6; i++) {
 var random = parseInt(Math.random() * allTypeArr.length);
 color += allTypeArr[random];
 }
 return color;
 }
 </script>
 </body>
</html>

Effect

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:
  • JavaScript+canvas realizes the jumping ball in the frame
  • JavaScript combined with Canvas to draw sports balls
  • Native js to realize moving ball (collision detection)
  • js realizes the movement of the ball in the specified area of ​​the page
  • Use js to realize the free movement code of the ball
  • js realizes a small ball that follows the mouse movement
  • JavaScript realizes the movement of a ball along a sine curve
  • P5.js Getting Started Tutorial: Ball Animation Example Code
  • Native js to realize bouncing ball
  • Native js realizes the bouncing ball effect

<<:  How to solve the problem of FileZilla_Server:425 Can't open data connection

>>:  Solution to primary key conflict when innodb_index_stats reports an error when importing backup data

Recommend

Installation tutorial of mysql5.7.21 decompression version under win10

Install the unzipped version of Mysql under win10...

MySQL 5.7.23 decompression version installation tutorial with pictures and text

It is too troublesome to find the installation tu...

Introduction to Linux compression and decompression commands

Table of contents Common compression formats: gz ...

CSS to achieve floating customer service effect

<div class="sideBar"> <div>...

HTML Basics: HTML Content Details

Let's start with the body: When viewing a web ...

9 ways to show and hide CSS elements

In web page production, displaying and hiding ele...

A brief talk about JavaScript variable promotion

Table of contents Preface 1. What variables are p...

Example of using supervisor to manage nginx+tomcat containers

need: Use docker to start nginx + tomcat dual pro...

Python 3.7 installation tutorial for MacBook

The detailed process of installing python3.7.0 on...

Implementation steps for installing Redis container in Docker

Table of contents Install Redis on Docker 1. Find...

Detailed explanation of how MySQL (InnoDB) handles deadlocks

1. What is deadlock? The official definition is a...

Mysql join query principle knowledge points

Mysql join query 1. Basic concepts Connect each r...

Tutorial on building a zookeeper server on Windows

Installation & Configuration The official web...

Sample code for implementing 3D book effect with CSS

Without further ado, let's take a look at the...