Canvas buffer
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 - 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);
// });
Developer | Darby Rathbone |
Username | blackkbot |
Uploaded | October 03, 2022 |
Rating | 3 |
Size | 2,791 Kb |
Views | 34,408 |
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!
Name | Size |
Java style console | 2,381 Kb |
Sphere detail | 2,558 Kb |
Perlin noise on worker | 2,989 Kb |
Jigsaw puzzle | 5,193 Kb |
Pendulum | 3,104 Kb |
Sudoku grid | 2,042 Kb |
Drag, drop and snap | 2,542 Kb |
Remote image | 1,989 Kb |
3d pure javascript 2d canvas | 2,600 Kb |
3d pure javascript 2d canvas | 2,667 Kb |
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!
Name | Username | Size |
Day 11 - Calendar Card | Arnellebalane | 6,984 Kb |
SVG hamburger menu button | Elifitch | 2,602 Kb |
Virtual vinyl | Davidpanik | 3,474 Kb |
Tab Menus | Zephyr | 3,180 Kb |
HTML5 Video Autoplay | Zivcos | 1,352 Kb |
Banner Ad | Raquellorenzana | 4,172 Kb |
IE11 Test | Boostnewmedia | 4,998 Kb |
Z-index demo | Kblh | 1,534 Kb |
Google Maps API Ground Overlay | Boycetrus | 2,961 Kb |
The Monty Hall Problem | Melatonind | 4,360 Kb |
Surf anonymously, prevent hackers from acquiring your IP address, send anonymous email, and encrypt your Internet connection. High speed, ultra secure, and easy to use. Instant setup. Hide Your IP Now!