In HTML5, the <canvas> tag is introduced for drawing graphics. <canvas> provides a canvas for drawing graphics and serves as a graphic container. The specific graphic drawing is done by JavaScript.
<!-- Draw bubble effect --><!DOCTYPEhtml><html><head><title>Canvas</title></head><styletype="text/css">#canvas{border: 1px solid #eee;}</style><body><canvasid="canvas"width="500"height="300"></canvas><!-- It is not recommended to use CSS to control the width, because if the set width and height are different from the initial ratio of 300:150, distortion may occur --><!-- If the browser does not support canvas, you can directly use <canvas>Your browser does not support canvas</canvas> and the browser will render alternative content --></body><scripttype="text/javascript"> class Bubble{ // ES6 added Class syntax
constructor(ctx){ // Constructor
this.colorList = [[254,158,159], [147,186,255], [217,153,249], [129,199,132], [255,202,98], [255,164,119]]; // Color scheme
this.ctx = ctx.getContext("2d"); // 2D drawing
this.circleList = []; // Bubble array
}
randomInt(a, b) { // Return a random number
return Number.parseInt(Math.random() * (b - a + 1) + a); // Get a random value between a and b, including a and b
}
newCircle(){ // New bubble
if(this.randomInt(0,50)) return 0; // Control the number of bubbles generated
var canvasHeight = this.ctx.canvas.height; // Get the canvas height
var circle = {}; // Define a new bubble object
circle.r = this.randomInt(10,50); // Random radius
circle.x = this.randomInt(0, this.ctx.canvas.width); // Initialize the X coordinate of the bubble
circle.xMoveSpeed = this.randomInt(-10,10); // X direction movement speed and direction
circle.y = canvasHeight + circle.r; // Initialize the Y coordinate of the bubble
circle.yMoveSpeed = this.randomInt(5,10); // Y direction movement speed
circle.color = this.colorList[this.randomInt(0,this.colorList.length-1)]; // Get the bubble color
this.circleList.push(circle); // Put the object into the array
}
destroyCircle(){// Destroy bubblethis.circleList.forEach((v, i)=>{if(v.y <-v.r)this.circleList.splice(i,1);// Destroy bubble object if it exceeds the upper boundary})}draw(){// Draw bubblethis.newCircle();// Call to generate new bubblethis.ctx.clearRect(0,0,this.ctx.canvas.width,this.ctx.canvas.height);// Clear the canvasthis.ctx.save();// Save the canvas statethis.circleList.forEach(v=>{this.ctx.beginPath();// Start a new paththis.ctx.fillStyle =`rgb(${v.color[0]},${v.color[1]},${v.color[2]},0.6)`;// Set the background colorthis.ctx.strokeStyle =`rgb(${v.color[0]},${v.color[1]},${v.color[2]})`;// Set the border colorthis.ctx.arc(v.x, v.y, v.r,0, Math.PI*2);// Draw a circle: x coordinate, y coordinate, radius, start angle, end angle, clockwise/counterclockwisethis.ctx.fill();// Fill the circlethis.ctx.stroke();// Draw the border v.y -= v.yMoveSpeed *0.1;// Move along the Y-axis v.x += v.xMoveSpeed *0.05;// Move along the X-axis})this.ctx.restore();// Restore the canvas statethis.destroyCircle();// Destroy bubblerequestAnimationFrame(()=>{this.draw();});// Recursive call}start(){// setInterval(() => {this.draw();},16.7); // Use setInterval to draw animation effectsrequestAnimationFrame(()=>{this.draw();});// Use requestAnimationFrame to draw images, based on the refresh rate (e.g. 60Hz, refresh every 16.7ms), requires recursive call// When the page is in an inactive state, the screen refresh task of the page will be paused by the system, so the requestAnimationFrame that follows the system's pace will also stop rendering. When the page is activated, the animation will continue from where it left off. setInterval needs to use visibilitychange listener to clear and reset the timer.}}(function(){var ctx = document.getElementById("canvas");// Get the canvas objectvar bubble =newBubble(ctx);// Instantiate Bubble bubble.start();// Start drawing})();</script></html>
Can save the resulting graphics in .png or .jpg format.
Most suitable for image-intensive games where many objects need to be redrawn frequently.
Once a graphic is drawn in Canvas, it no longer receives attention from the browser. If its position changes, the graphic needs to be redrawn, including any objects that have been covered by the graphic.