Canvas Falloff Test

Developer
Size
2,989 Kb
Views
4,048

How do I make an canvas falloff test?

What is a canvas falloff test? How do you make a canvas falloff test? This script and codes were developed by Aaron Levine on 30 January 2023, Monday.

Canvas Falloff Test Previews

Canvas Falloff Test - Script Codes HTML Codes

<!DOCTYPE html>
<html >
<head> <meta charset="UTF-8"> <title>Canvas Falloff Test</title> <script src="https://s.codepen.io/assets/libs/modernizr.js" type="text/javascript"></script>
<meta name="viewport" content="width=device-width"> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/5.0.0/normalize.min.css"> <link rel="stylesheet" href="css/style.css">
</head>
<body> <script src='http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js'></script> <script src="js/index.js"></script>
</body>
</html>

Canvas Falloff Test - Script Codes CSS Codes

canvas { width: 100%; height: 100%; background: rgba(0, 0, 0, 0.2);
}
html, body { width: 100%; height: 100%; padding: 0; overflow: hidden;
}

Canvas Falloff Test - Script Codes JS Codes

var App = {};
App.canvas;
App.ctx;
App.width;
App.height;
App.prevTimestamp;
App.deltaTime;
App.mouseX;
App.mouseY;
App.dots = [];
window.requestAnimationFrame = window.requestAnimationFrame || window.mozRequestAnimationFrame || window.webkitRequestAnimationFrame;
App.Clear = function(){ App.ctx.clearRect(0, 0, App.width, App.height);
}
App.Dot = function(x, y, color){ this.x = x; this.y = y; this.offsetX = 0; this.offsetY = 0; this.width = 10; this.height = 10; this.color = color;
};
App.Dot.prototype.Draw = function(){ var ctx = App.ctx; var x = this.x + this.offsetX; var y = this.y + this.offsetY; ctx.save(); ctx.fillStyle = this.color; ctx.fillRect(x - this.width/2, y - this.height/2, this.width, this.height); ctx.restore();
};
App.InitCanvas = function(){ var canvas = App.canvas = document.createElement('canvas'); document.body.appendChild(canvas); App.UpdateDimensions(); App.ctx = canvas.getContext('2d');
};
App.UpdateDimensions = function(){ var rect = App.canvas.getBoundingClientRect(); var width = App.width = App.canvas.width = rect.width; var height = App.height = App.canvas.height = rect.height;
};
App.InitDots = function(){ for(var x=0; x<App.width; x += 20){ for(var y=0; y<App.height; y+= 20){ App.dots.push(new App.Dot(x, y, 'rgb(0, 0, 0)')); } }
}
App.InitMouseEvents = function(){ App.mouseX = App.width / 2; App.mouseY = App.height / 2; App.canvas.addEventListener('mousemove', function(e){ App.mouseX = e.offsetX; App.mouseY = e.offsetY; }); App.canvas.addEventListener('touchmove', function(e){ App.mouseX = e.touches[0].pageX; App.mouseY = e.touches[0].pageY; }); document.addEventListener('touchmove', function(e){ e.returnValue = false; if(typeof e.preventDefault === 'function') e.preventDefault(); });
}
App.MoveDots = function(){ for(var i=0, j=App.dots.length; i<j; i++){ var dot = App.dots[i]; var difX = dot.x - App.mouseX; var difY = dot.y - App.mouseY; var distanceSquared = Math.pow(difX, 2) + Math.pow(difY, 2); var distance = Math.sqrt(distanceSquared); var dirX = difX / distance; var dirY = difY / distance; dot.offsetX = dirX * Math.log(distance * 10000000); dot.offsetY = dirY * Math.log(distance * 10000000); dot.offsetX = Math.min(50, Math.max(-50, dot.offsetX)); dot.offsetY = Math.min(50, Math.max(-50, dot.offsetY)); var size = Math.max(1, Math.min(18, Math.log(distance / 15)*4 ) ); dot.width = dot.height = size; var red = 255 - (size / 18) * 255; dot.color = 'rgb('+(red.toFixed())+', 0, 0)'; }
}
App.Draw = function(){ for(var i=0, j=App.dots.length; i<j; i++){ App.dots[i].Draw(); }
}
App.Loop = function(timestamp){ App.deltaTime = timestamp - App.prevTimestamp; App.prevTimestamp = timestamp; App.Clear(); App.MoveDots(); App.Draw(); requestAnimationFrame(App.Loop);
}
App.Main = function(){ App.InitCanvas(); App.InitDots(); App.InitMouseEvents(); App.Loop();
};
App.Main();
Canvas Falloff Test - Script Codes
Canvas Falloff Test - Script Codes
Home Page Home
Developer Aaron Levine
Username Aldlevine
Uploaded January 30, 2023
Rating 4
Size 2,989 Kb
Views 4,048
Do you need developer help for Canvas Falloff Test?

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!

Aaron Levine (Aldlevine) Script Codes
Create amazing SEO content 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!