Canvas buffer

Size
2,791 Kb
Views
34,408

How do I make an canvas buffer?

I draw a simple shape on one canvas and then i redraw it on an another canvas after performing transformations so it doesn't have to be rendered again.. What is a canvas buffer? How do you make a canvas buffer? This script and codes were developed by Darby Rathbone on 03 October 2022, Monday.

Canvas buffer Previews

Canvas buffer - Script Codes HTML Codes

<!DOCTYPE html>
<html >
<head> <meta charset="UTF-8"> <title>canvas buffer </title> <link rel="stylesheet" href="css/style.css">
</head>
<body> <script src="js/index.js"></script>
</body>
</html>

Canvas buffer - Script Codes CSS Codes

html, body { width: 100%; height: 100%; margin: 0px;
}
canvas{ position:absolute; top:0px; left:0px; bottom:0px; right:0px;
}

Canvas buffer - Script Codes JS Codes

var Vector = (function () { Vector.add = function (v1, v2) { return new Vector(v1.x + v2.x, v1.y + v2.y); }; Vector.sub = function (v1, v2) { return new Vector(v1.x - v2.x, v1.y - v2.y); }; Vector.project = function (v1, v2) { return v1.clone().scale((v1.dot(v2)) / v1.magSq()); }; function Vector(x, y) { this.x = x !== null ? x : 0; this.y = y !== null ? y : 0; } Vector.prototype.set = function (x, y) { this.x = x; this.y = y; return this; }; Vector.prototype.add = function (v) { this.x += v.x; this.y += v.y; return this; }; Vector.prototype.sub = function (v) { this.x -= v.x; this.y -= v.y; return this; }; Vector.prototype.scale = function (f) { this.x *= f; this.y *= f; return this; }; Vector.prototype.dot = function (v) { return this.x * v.x + this.y * v.y; }; Vector.prototype.cross = function (v) { return (this.x * v.y) - (this.y * v.x); }; Vector.prototype.mag = function () { return Math.sqrt(this.x * this.x + this.y * this.y); }; Vector.prototype.magSq = function () { return this.x * this.x + this.y * this.y; }; Vector.prototype.dist = function (v) { var dx, dy; dx = v.x - this.x; dy = v.y - this.y; return Math.sqrt(dx * dx + dy * dy); }; Vector.prototype.distSq = function (v) { var dx, dy; dx = v.x - this.x; dy = v.y - this.y; return dx * dx + dy * dy; }; Vector.prototype.norm = function () { var m; m = Math.sqrt(this.x * this.x + this.y * this.y); this.x /= m; this.y /= m; return this; }; Vector.prototype.limit = function (l) { var m, mSq; mSq = this.x * this.x + this.y * this.y; if (mSq > l * l) { m = Math.sqrt(mSq); this.x /= m; this.y /= m; this.x *= l; this.y *= l; return this; } }; Vector.prototype.copy = function (v) { this.x = v.x; this.y = v.y; return this; }; Vector.prototype.clone = function () { return new Vector(this.x, this.y); }; Vector.prototype.clear = function () { this.x = 0; this.y = 0; return this; }; return Vector;
})();
var force = function(o1,o2,v){ var o1m = o1.getMass(); var o2m = o2.getMass(); var tm = o1m+o2m; o1.velocity.x += o1m*v.x/tm; o1.velocity.y += o1m*v.y/tm; o2.velocity.x -= o2m*v.x/tm; o2.velocity.y -= o2m*v.y/tm;
};
var canvas2 = document.createElement('canvas');
var context2 = canvas2.getContext('2d');
canvas2.width =700;
canvas2.height = 700;
canvas2.style.border = "5px ridge";
canvas2.style.padding = "auto";
canvas2.style.margin = 'auto';
canvas2.style.position = "fixed";
canvas2.style.bottom = "0px";
canvas2.style.right = "0px";
canvas2.style.top = "0px";
canvas2.style.left = "0px";
var canvas = document.createElement('canvas');
var context = canvas.getContext('2d');
//document.body.style['overflow'] = 'hidden';
canvas.width =700;
canvas.height = 700;
context.lineWidth =5;
context.strokeStyle = "#111";
canvas.style.border = "5px ridge";
canvas.style.padding = "auto";
canvas.style.margin = 'auto';
canvas.style.position = "fixed";
canvas.style.bottom = "0px";
canvas.style.right = "0px";
canvas.style.top = "0px";
canvas.style.left = "0px";
document.body.appendChild(canvas);
context.save();
context.translate(canvas.width/2.0,canvas.height/2.0);
for(var i =0;i<360;i+=5){ //context.translate(i0,i); context.beginPath(); context.rotate(i*Math.PI/25.0); //context.moveTo(0,0); context.save(); for(var j =0.0;j<175;j+=5){ if(j<175/2) context.rotate(-j/1080.0); else context.rotate(-(j-(i/10))/1080.0); context.lineTo(j,j); context.moveTo(j,j); } context.closePath(); context.restore(); //context.fill(); context.stroke(); context.rotate(i*Math.PI/25.0); //context.translate(-i,-i);
}
context2.drawImage(canvas,0,0);
requestAnimationFrame(function animator(){ context.restore();
context.globalAlpha =.1; context.fillStyle = "#fff"; context.fillRect(0,0,canvas.width,canvas.height); context.translate(canvas.width/2.0,canvas.height/2.0); context.rotate(Math.PI/(2*90.0)); context.translate(-canvas.width/2.0,-canvas.height/2.0); //context.translate(-canvas.width/2.0,-canvas.height/2.0); context.globalAlpha = 1; context.drawImage(canvas2,0,0); requestAnimationFrame(animator);
// });
Canvas buffer - Script Codes
Canvas buffer - Script Codes
Home Page Home
Developer Darby Rathbone
Username blackkbot
Uploaded October 03, 2022
Rating 3
Size 2,791 Kb
Views 34,408
Do you need developer help for Canvas buffer?

Find the perfect freelance services for your business! Fiverr's mission is to change how the world works together. Fiverr connects businesses with freelancers offering digital services in 500+ categories. Find Developer!

Darby Rathbone (blackkbot) Script Codes
Create amazing art & images with AI!

Jasper is the AI Content Generator that helps you and your team break through creative blocks to create amazing, original content 10X faster. Discover all the ways the Jasper AI Content Platform can help streamline your creative workflows. Start For Free!