Canvas implements the effect code of dynamic ball overlapping
In the JavaScript sports series, various sports are introduced in detail, including wall sports. However, if you use canvas to implement it, it is another way of thinking. This article will introduce in detail the dynamic ball overlapping effect of canvas. Let’s take a look at it together
Previous words
In the javascript sports series, various sports are introduced in detail, including wall sports. However, if you use canvas to implement it, it is another way of thinking. This article will introduce in detail the overlapping effect of canvas dynamic balls
Static balls
First, generate 50 static balls with random radius and random position
<button id="btn">按钮</button> <canvas id="canvas" width="500" height="300" style="border:1px solid black">当前浏览器不支持canvas,请更换浏览器后再试</canvas> <script> var canvas = document.getElementById('canvas'); var H=300,W=500; btn.onclick = function(){ getBalls(); } getBalls(); function getBalls(){ canvas.height = H; if(canvas.getContext){ var cxt = canvas.getContext('2d'); for(var i = 0; i < 50; i++){ var tempR = Math.floor(Math.random()*255); var tempG = Math.floor(Math.random()*255); var tempB = Math.floor(Math.random()*255); cxt.fillStyle = 'rgb(' + tempR + ',' + tempG + ',' + tempB + ')'; var tempW = Math.floor(Math.random()*W); var tempH = Math.floor(Math.random()*H); var tempR = Math.floor(Math.random()*50); cxt.beginPath(); cxt.arc(tempW,tempH,tempR,0,Math.PI*2); cxt.fill(); } } } </script>
Random movement
Then, these 50 balls move randomly, and the movement status of the balls needs to be updated in conjunction with the timer. At this time, the above code needs to be rewritten
<button id="btn">更新</button> <canvas id="canvas" width="500" height="300" style="border:1px solid black">当前浏览器不支持canvas,请更换浏览器后再试</canvas> <script> btn.onclick = function(){history.go();} var canvas = document.getElementById('canvas'); //存储画布宽高 var H=300,W=500; //存储小球个数 var NUM = 50; //存储小球 var balls = []; function getBalls(){ if(canvas.getContext){ var cxt = canvas.getContext('2d'); for(var i = 0; i < NUM; i++){ var tempR = Math.floor(Math.random()*255); var tempG = Math.floor(Math.random()*255); var tempB = Math.floor(Math.random()*255); var tempColor = 'rgb(' + tempR + ',' + tempG + ',' + tempB + ')'; var tempX = Math.floor(Math.random()*W); var tempY = Math.floor(Math.random()*H); var tempR = Math.floor(Math.random()*30+20); var tempBall = { x:tempX, y:tempY, r:tempR, stepX:Math.floor(Math.random() * 4 -2), stepY:Math.floor(Math.random() * 4 -2), color:tempColor, disX:Math.floor(Math.random() * 3 -1), disY:Math.floor(Math.random() * 3 -1) }; balls.push(tempBall); } } } function updateBalls(){ for(var i = 0; i < balls.length; i++){ balls[i].stepY += balls[i].disY; balls[i].stepX += balls[i].disX; balls[i].x += balls[i].stepX; balls[i].y += balls[i].stepY; } } function renderBalls(){ //重置画布高度,达到清空画布的效果 canvas.height = H; if(canvas.getContext){ var cxt = canvas.getContext('2d'); for(var i = 0; i < balls.length; i++){ cxt.beginPath(); cxt.arc(balls[i].x,balls[i].y,balls[i].r,0,2*Math.PI); cxt.fillStyle = balls[i].color; cxt.closePath(); cxt.fill(); } } } getBalls(); clearInterval(oTimer); var oTimer = setInterval(function(){ //更新小球运动状态 updateBalls(); //渲染小球 renderBalls(); },50); </script>
Block detection
Next, add the ball collision detection Function, when the ball hits the wall, it changes to the opposite direction
function bumpTest(ele){ //左侧 if(ele.x <= ele.r){ ele.x = ele.r; ele.stepX = -ele.stepX; } //右侧 if(ele.x >= W - ele.r){ ele.x = W - ele.r; ele.stepX = -ele.stepX; } //上侧 if(ele.y <= ele.r){ ele.y = ele.r; ele.stepY = -ele.stepY; } //下侧 if(ele.y >= H - ele.r){ ele.y = H - ele.r; ele.stepY = -ele.stepY; } }
<script> btn.onclick = function(){history.go();} var canvas = document.getElementById('canvas'); //存储画布宽高 var H=300,W=500; //存储小球个数 var NUM = 30; //存储小球 var balls = []; function getBalls(){ if(canvas.getContext){ var cxt = canvas.getContext('2d'); for(var i = 0; i < NUM; i++){ var tempR = Math.floor(Math.random()*255); var tempG = Math.floor(Math.random()*255); var tempB = Math.floor(Math.random()*255); var tempColor = 'rgb(' + tempR + ',' + tempG + ',' + tempB + ')'; var tempR = Math.floor(Math.random()*30+20); var tempX = Math.floor(Math.random()*(W-tempR) + tempR); var tempY = Math.floor(Math.random()*(H-tempR) + tempR); var tempBall = { x:tempX, y:tempY, r:tempR, stepX:Math.floor(Math.random() * 13 -6), stepY:Math.floor(Math.random() * 13 -6), color:tempColor }; balls.push(tempBall); } } } function updateBalls(){ for(var i = 0; i < balls.length; i++){ balls[i].x += balls[i].stepX; balls[i].y += balls[i].stepY; bumpTest(balls[i]); } } function bumpTest(ele){ //左侧 if(ele.x <= ele.r){ ele.x = ele.r; ele.stepX = -ele.stepX; } //右侧 if(ele.x >= W - ele.r){ ele.x = W - ele.r; ele.stepX = -ele.stepX; } //上侧 if(ele.y <= ele.r){ ele.y = ele.r; ele.stepY = -ele.stepY; } //下侧 if(ele.y >= H - ele.r){ ele.y = H - ele.r; ele.stepY = -ele.stepY; } } function renderBalls(){ //重置画布高度,达到清空画布的效果 canvas.height = H; if(canvas.getContext){ var cxt = canvas.getContext('2d'); for(var i = 0; i < balls.length; i++){ cxt.beginPath(); cxt.arc(balls[i].x,balls[i].y,balls[i].r,0,2*Math.PI); cxt.fillStyle = balls[i].color; cxt.closePath(); cxt.fill(); } } } getBalls(); clearInterval(oTimer); var oTimer = setInterval(function(){ //更新小球运动状态 updateBalls(); //渲染小球 renderBalls(); },50); </script>
Overlapping effect
canvas的合成属性globalCompositeOperation表示后绘制的图形怎样与先绘制的图形结合,属性值是字符串,可能值如下: source-over(默认):后绘制的图形位于先绘制的图形上方 source-in:后绘制的图形与先绘制的图形重叠的部分可见,两者其他部分完全透明 source-out:后绘制的图形与先绘制的图形不重叠的部分可见,先绘制的图形完全透明 source-atop:后绘制的图形与先绘制的图形重叠的部分可见,先绘制的图形不受影响 destination-over:后绘制的图形位于先绘制的图形下方,只有之前透明像素下的部分才可见 destination-in:后绘制的图形位于先绘制的图形下方,两者不重叠的部分完全透明 destination-out:后绘制的图形擦除与先绘制的图形重叠的部分 destination-atop:后绘制的图形位于先绘制的图形下方,在两者不重叠的地方,先绘制的图形会变透明 lighter:后绘制的图形与先绘制的图形重叠部分的值相加,使该部分变亮 copy:后绘制的图形完全替代与之重叠的先绘制图形 xor:后绘制的图形与先绘制的图形重叠的部分执行"异或"操作
Add the overlapping effect of the ball to 'xor', which is the final effect display
<script> btn.onclick = function(){history.go();} var canvas = document.getElementById('canvas'); //存储画布宽高 var H=300,W=500; //存储小球个数 var NUM = 30; //存储小球 var balls = []; function getBalls(){ if(canvas.getContext){ var cxt = canvas.getContext('2d'); for(var i = 0; i < NUM; i++){ var tempR = Math.floor(Math.random()*255); var tempG = Math.floor(Math.random()*255); var tempB = Math.floor(Math.random()*255); var tempColor = 'rgb(' + tempR + ',' + tempG + ',' + tempB + ')'; var tempR = Math.floor(Math.random()*30+20); var tempX = Math.floor(Math.random()*(W-tempR) + tempR); var tempY = Math.floor(Math.random()*(H-tempR) + tempR); var tempBall = { x:tempX, y:tempY, r:tempR, stepX:Math.floor(Math.random() * 21 -10), stepY:Math.floor(Math.random() * 21 -10), color:tempColor }; balls.push(tempBall); } } } function updateBalls(){ for(var i = 0; i < balls.length; i++){ balls[i].x += balls[i].stepX; balls[i].y += balls[i].stepY; bumpTest(balls[i]); } } function bumpTest(ele){ //左侧 if(ele.x <= ele.r){ ele.x = ele.r; ele.stepX = -ele.stepX; } //右侧 if(ele.x >= W - ele.r){ ele.x = W - ele.r; ele.stepX = -ele.stepX; } //上侧 if(ele.y <= ele.r){ ele.y = ele.r; ele.stepY = -ele.stepY; } //下侧 if(ele.y >= H - ele.r){ ele.y = H - ele.r; ele.stepY = -ele.stepY; } } function renderBalls(){ //重置画布高度,达到清空画布的效果 canvas.height = H; if(canvas.getContext){ var cxt = canvas.getContext('2d'); for(var i = 0; i < balls.length; i++){ cxt.beginPath(); cxt.arc(balls[i].x,balls[i].y,balls[i].r,0,2*Math.PI); cxt.fillStyle = balls[i].color; cxt.globalCompositeOperation = 'xor'; cxt.closePath(); cxt.fill(); } } } getBalls(); clearInterval(oTimer); var oTimer = setInterval(function(){ //更新小球运动状态 updateBalls(); //渲染小球 renderBalls(); },50); </script>
The above is the entire content of this article. I hope it will be helpful to everyone's study. For more related content, please pay attention to the PHP Chinese website!
Related recommendations:
canvas realizes the effect of love and rainbow rain
canvas draws various basic graphics
The above is the detailed content of Canvas implements the effect code of dynamic ball overlapping. For more information, please follow other related articles on the PHP Chinese website!

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

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

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

Hot Topics

Schools using canvas include Stanford University, MIT, Columbia University, University of California, Berkeley, etc. Detailed introduction: 1. Stanford University uses Canvas as its main online learning platform. Teachers and students at Stanford University use Canvas to manage and communicate course content, and learn through functions such as online discussions, assignment submissions, and exams; 2. Ma Provincial Polytechnic Institute and MIT also use Canvas as their online learning management system and conduct course management through the Canvas platform; 3. Columbia University, etc.

The canvas arrow plug-ins include: 1. Fabric.js, which has a simple and easy-to-use API and can create custom arrow effects; 2. Konva.js, which provides the function of drawing arrows and can create various arrow styles; 3. Pixi.js , which provides rich graphics processing functions and can achieve various arrow effects; 4. Two.js, which can easily create and control arrow styles and animations; 5. Arrow.js, which can create various arrow effects; 6. Rough .js, you can create hand-drawn arrows, etc.

Explore the Canvas framework: To understand what are the commonly used Canvas frameworks, specific code examples are required. Introduction: Canvas is a drawing API provided in HTML5, through which we can achieve rich graphics and animation effects. In order to improve the efficiency and convenience of drawing, many developers have developed different Canvas frameworks. This article will introduce some commonly used Canvas frameworks and provide specific code examples to help readers gain a deeper understanding of how to use these frameworks. 1. EaselJS framework Ea

The details of the canvas clock include clock appearance, tick marks, digital clock, hour, minute and second hands, center point, animation effects, other styles, etc. Detailed introduction: 1. Clock appearance, you can use Canvas to draw a circular dial as the appearance of the clock, and you can set the size, color, border and other styles of the dial; 2. Scale lines, draw scale lines on the dial to represent hours or minutes. Position; 3. Digital clock, you can draw a digital clock on the dial to indicate the current hour and minute; 4. Hour hand, minute hand, second hand, etc.

The versions of html2canvas include html2canvas v0.x, html2canvas v1.x, etc. Detailed introduction: 1. html2canvas v0.x, which is an early version of html2canvas. The latest stable version is v0.5.0-alpha1. It is a mature version that has been widely used and verified in many projects; 2. html2canvas v1.x, this is a new version of html2canvas.

How to use canvas to draw charts and animation effects in uniapp requires specific code examples 1. Introduction With the popularity of mobile devices, more and more applications need to display various charts and animation effects on the mobile terminal. As a cross-platform development framework based on Vue.js, uniapp provides the ability to use canvas to draw charts and animation effects. This article will introduce how uniapp uses canvas to achieve chart and animation effects, and give specific code examples. 2. canvas

The tkinter canvas attributes include bg, bd, relief, width, height, cursor, highlightbackground, highlightcolor, highlightthickness, insertbackground, insertwidth, selectbackground, selectforeground, xscrollcommand attributes, etc. Detailed introduction

Understand the power and application of canvas in game development Overview: With the rapid development of Internet technology, web games are becoming more and more popular among players. As an important part of web game development, canvas technology has gradually emerged in game development, showing its powerful power and application. This article will introduce the potential of canvas in game development and demonstrate its application through specific code examples. 1. Introduction to canvas technology Canvas is a new element in HTML5, which allows us to use
