Perspective

Developer
Size
2,894 Kb
Views
30,360

How do I make an perspective?

Playing with projecting rays through known points to achieve faux perspective.. What is a perspective? How do you make a perspective? This script and codes were developed by Tristan on 25 August 2022, Thursday.

Perspective Previews

Perspective - Script Codes HTML Codes

<!DOCTYPE html>
<html >
<head> <meta charset="UTF-8"> <title>Perspective</title> <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>

Perspective - Script Codes CSS Codes

*{margin:0;padding:0;-moz-box-sizing:border-box;-webkit-box-sizing:border-box}
body{font-size:75%;background:#222;font-family:arial;padding:20px 0 0 20px}
canvas{border:1px solid #181818;display:block;box-shadow:0 0 10px #111}

Perspective - Script Codes JS Codes

var canvas, c, w, h, twoPI = Math.PI * 2, mX, mY, resize = true, mousemove = true, per = {};
window.requestAnimFrame=function(){return window.requestAnimationFrame||window.webkitRequestAnimationFrame||window.mozRequestAnimationFrame||window.oRequestAnimationFrame||window.msRequestAnimationFrame||function(a){window.setTimeout(a,1E3/60)}}();
window.onload = function(){ canvas = document.createElement('canvas') w = canvas.width = window.innerWidth - 40; h = canvas.height = window.innerHeight - 40; c = canvas.getContext('2d'); document.body.appendChild(canvas); !resize || window.addEventListener('resize', function(e){ w = canvas.width = window.innerWidth - 40; h = canvas.height = window.innerHeight - 40; P.reset();}); !mousemove || canvas.addEventListener('mousemove', function(e){ per.x = mX = e.pageX-20; per.y = mY = e.pageY-20; }); per = { x: w/2, y: h/2 }; var P = new Perspective(10, 10, 10);
} function Perspective(xc, yc, size){ var points = [], padding = 1.5; function init(xc, yc, size){ var halfX = w/2 - xc*size*padding/2, halfY = h/2 - yc*size*padding/2; for(var y = 1; y <= yc; y++){ for(var x = 1; x <= xc; x++){ points.push( { x: (x * size * padding) + halfX, y: (y * size * padding) + halfY, size: size, xd: Math.random() * 4 - 2, yd: Math.random() * 4 - 2 } ); } } mX = w/2; mY = h/2; } function update(){ for(var p = 0; p < points.length; p++){ points[p].x += points[p].xd; if(points[p].x >= w) points[p].xd *= -1; if(points[p].x <= 0) points[p].xd *= -1; points[p].y += points[p].yd; if(points[p].y >= h) points[p].yd *= -1; if(points[p].y <= 0) points[p].yd *= -1; } } function clear(){ c.save(); c.fillStyle = "rgba(0,64,128,0.7)"; c.fillRect(0,0,w,h); c.restore(); } function draw(){ c.save(); c.globalCompositeOperation = "lighter"; c.strokeStyle = "rgba(255,255,255,0.05)"; c.fillStyle = "rgba(0,255,255,0.05)"; for(var p = 0; p < points.length; p++){ c.beginPath(); var hsize = points[p].size / 2, x1 = points[p].x - hsize, y1 = points[p].y - hsize, x2 = points[p].x + hsize, y2 = points[p].y + hsize; c.moveTo(x1, y1); c.lineTo(x2, y1); c.lineTo(x2, y2); c.lineTo(x1, y2); c.closePath(); c.fill(); c.stroke(); var a1 = Math.atan2(y1 - per.y, x1 - per.x), a2 = Math.atan2(y1 - per.y, x2 - per.x), a3 = Math.atan2(y2 - per.y, x2 - per.x), a4 = Math.atan2(y2 - per.y, x1 - per.x); var xd = mX - points[p].x, yd = mY - points[p].y, dd = Math.sqrt(xd * xd + yd * yd)/5; c.beginPath(); c.moveTo(x1, y1); c.lineTo(x1 + Math.cos(a1) * dd, y1 + Math.sin(a1) * dd); c.lineTo(x2 + Math.cos(a2) * dd, y1 + Math.sin(a2) * dd); c.lineTo(x2, y1); c.closePath(); c.fill(); c.stroke(); c.beginPath(); c.moveTo(x2, y1); c.lineTo(x2 + Math.cos(a2) * dd, y1 + Math.sin(a2) * dd); c.lineTo(x2 + Math.cos(a3) * dd, y2 + Math.sin(a3) * dd); c.lineTo(x2, y2); c.closePath(); c.fill(); c.stroke(); c.beginPath(); c.moveTo(x2, y2); c.lineTo(x2 + Math.cos(a3) * dd, y2 + Math.sin(a3) * dd); c.lineTo(x1 + Math.cos(a4) * dd, y2 + Math.sin(a4) * dd); c.lineTo(x1, y2); c.closePath(); c.fill(); c.stroke(); c.beginPath(); c.moveTo(x1, y2); c.lineTo(x1 + Math.cos(a4) * dd, y2 + Math.sin(a4) * dd); c.lineTo(x1 + Math.cos(a1) * dd, y1 + Math.sin(a1) * dd); c.lineTo(x1, y1); c.closePath(); c.fill(); c.stroke(); c.beginPath(); c.moveTo(x1 + Math.cos(a1) * dd, y1 + Math.sin(a1) * dd); c.lineTo(x2 + Math.cos(a2) * dd, y1 + Math.sin(a2) * dd); c.lineTo(x2 + Math.cos(a3) * dd, y2 + Math.sin(a3) * dd); c.lineTo(x1 + Math.cos(a4) * dd, y2 + Math.sin(a4) * dd); c.closePath(); c.fill(); c.stroke(); } c.restore(); } function animate(){ update(); clear(); draw(); window.requestAnimFrame(animate); } this.reset = function(){ for(var p = 0; p < points.length; p++){ var halfX = w/2 - xc*size*padding/2, halfY = h/2 - yc*size*padding/2, p = 0; for(var y = 1; y <= yc; y++){ for(var x = 1; x <= xc; x++){ points[p].x = (x * size * padding) + halfX; points[p].y = (y * size * padding) + halfY; p++; } } } } init(xc, yc, size); window.requestAnimFrame(animate); }
Perspective - Script Codes
Perspective - Script Codes
Home Page Home
Developer Tristan
Username sinthetyc
Uploaded August 25, 2022
Rating 4
Size 2,894 Kb
Views 30,360
Do you need developer help for Perspective?

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!

Tristan (sinthetyc) Script Codes
Create amazing web 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!