Fake 3D

Developer
Size
3,086 Kb
Views
38,456

How do I make an fake 3d?

Playing with 3d effect in canvas. What is a fake 3d? How do you make a fake 3d? This script and codes were developed by Massimo on 17 September 2022, Saturday.

Fake 3D Previews

Fake 3D - Script Codes HTML Codes

<!DOCTYPE html>
<html >
<head> <meta charset="UTF-8"> <title>Fake 3D</title> <link rel="stylesheet" href="css/style.css">
</head>
<body>
<canvas id="c"></canvas> <script src='https://cdnjs.cloudflare.com/ajax/libs/dat-gui/0.6.2/dat.gui.min.js'></script> <script src="js/index.js"></script>
</body>
</html>

Fake 3D - Script Codes CSS Codes

@import url("https://fonts.googleapis.com/css?family=Open+Sans");
body { margin: 0; overflow: hidden; background: #000;
}
body li, body .close-button { font-family: 'Open Sans', sans-serif; font-size: 14px; text-transform: capitalize;
}

Fake 3D - Script Codes JS Codes

var c = document.getElementById('c'), ctx = c.getContext('2d'), shapes = [], s = { 'count': 100, 'size': 20, 'speed': 10, 'focal': 300, 'perspective': 5000 };
window.requestAnimFrame = (function () { return window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || window.oRequestAnimationFrame || window.msRequestAnimationFrame || function (callback) { window.setTimeout(callback, 1000/60); };
})();
function resize() { c.width = window.innerWidth; c.height = window.innerHeight; ctx.translate(.5*c.width, .5*c.height); shapes = []; init();
}
resize();
window.addEventListener("resize", resize);
var gui = new dat.GUI();
gui.add(s, "count", 1, 300, 1).onChange(resize);
gui.add(s, "size", 1, 100, 1).onChange(resize);
gui.add(s, "speed", 1, 50, 1);
gui.add(s, "focal", 1, 1000, 1);
gui.add(s, "perspective", 1, 10000, 1);
function Shape(x, y, z, col, shape) { this.x = x; this.y = y; this.z = z; this.col = col; this.shape = shape;
}
function setColor () { return 'rgba(' + Math.round(255*Math.random()) + ',' + Math.round(255*Math.random()) + ',' + Math.round(255*Math.random()) + ',' + Math.random() ')';
}
function setShape(v) { var rand = Math.random(), shape; if(rand < .3) { shape = 'rect'; } else if(rand > .3 && rand < .7) { shape = 'triangle'; } else { shape = 'circle'; } return shape;
}
function drawShape(shape) { ctx.beginPath(); if(shape == 'rect') { ctx.rect(-s.size/2, -s.size/2, s.size, s.size); } else if(shape == 'triangle') { ctx.moveTo(0, 0); ctx.lineTo(s.size/2, -s.size); ctx.lineTo(s.size, 0); } else { ctx.arc(0, 0, s.size, 0, 2*Math.PI); } ctx.fill();
}
function init() { for(var i=0; i<s.count; i++) { var x = -c.width/2 + c.width*Math.random(), y = -c.height/2 + c.height*Math.random(), z = s.perspective*Math.random(), col = setColor(), shape = setShape(Math.random()); shapes.push(new Shape(x, y, z, col, shape)); }
}
function update() { ctx.clearRect(-.5*c.width, -.5*c.height, c.width, c.height); for(var i=0; i<shapes.length; i++) { var sh = shapes[i]; ctx.save(); var p = s.focal / (s.focal + sh.z); ctx.translate(p*sh.x, p*sh.y); ctx.scale(p, p); ctx.fillStyle = sh.col; drawShape(sh.shape); ctx.restore(); sh.z -= s.speed; if(sh.z < -s.focal) { sh.z = s.perspective*Math.random(); } } window.requestAnimFrame(update);
}
update();
Fake 3D - Script Codes
Fake 3D - Script Codes
Home Page Home
Developer Massimo
Username _massimo
Uploaded September 17, 2022
Rating 4.5
Size 3,086 Kb
Views 38,456
Do you need developer help for Fake 3D?

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!

Massimo (_massimo) Script Codes
Create amazing sales emails 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!