Canvas Optical Illusion

Developer
Size
2,148 Kb
Views
26,312

How do I make an canvas optical illusion?

Recreation of an optical illusion from a reddit thread.https://www.reddit.com/r/pics/comments/52bs6k/this_isnt_a_gif_your_eyes_just_cant_see_all_12/. What is a canvas optical illusion? How do you make a canvas optical illusion? This script and codes were developed by Kevin on 27 August 2022, Saturday.

Canvas Optical Illusion Previews

Canvas Optical Illusion - Script Codes HTML Codes

<!DOCTYPE html>
<html >
<head> <meta charset="UTF-8"> <title>Canvas Optical Illusion</title> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="css/style.css">
</head>
<body> <canvas id = "canvas"></canvas> <script src='https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.0/jquery.min.js'></script> <script src="js/index.js"></script>
</body>
</html>

Canvas Optical Illusion - Script Codes CSS Codes

body{ margin: 0;
}
#canvas { display: block; height: 100vh; width: 100vw;
}

Canvas Optical Illusion - Script Codes JS Codes

function draw() { var canvas = document.getElementById("canvas"); canvas.width = window.innerWidth; canvas.height = window.innerHeight; window.addEventListener('resize', resizeCanvas, false); function resizeCanvas() { canvas.width = window.innerWidth; canvas.height = window.innerHeight; start(); } if (canvas.getContext) { var ctx = canvas.getContext("2d"); function start() { var size = canvas.width / 20; ctx.strokeStyle = "gray"; ctx.lineWidth = 6; for (var x = 0; x < canvas.width; x += size) { ctx.beginPath(); ctx.moveTo(x, 0); ctx.lineTo(x, canvas.height); ctx.stroke(); } for (var x = 0; x < canvas.height; x += size) { ctx.beginPath(); ctx.moveTo(0, x); ctx.lineTo(canvas.width, x); ctx.stroke(); } for (var x = 0; x < canvas.width; x += size) { ctx.beginPath(); ctx.moveTo(x, 0); ctx.lineTo(x - canvas.height, canvas.height); ctx.stroke(); } for (var x = 0; x < canvas.height; x += size) { ctx.beginPath(); ctx.moveTo(0, x); ctx.lineTo(canvas.height - x, canvas.height); ctx.stroke(); } for (var x = 0; x < canvas.width; x += size) { ctx.beginPath(); ctx.moveTo(x, 0); ctx.lineTo(x + canvas.height, canvas.height); ctx.stroke(); } for (var x = 0; x < canvas.height; x += size) { ctx.beginPath(); ctx.moveTo(canvas.width, x); ctx.lineTo(canvas.width - canvas.height + x, canvas.height); ctx.stroke(); } for (var x = 0; x < canvas.width; x += (canvas.width / 20) * 2) { for (var z = 0; z < canvas.height; z += (canvas.width / 20) * 2) { ctx.beginPath(); ctx.fillStyle = "white"; ctx.arc(x, z, 6, 0, Math.PI * 2); ctx.fill(); ctx.beginPath(); ctx.fillStyle = "black"; ctx.arc(x, z, 5, 0, Math.PI * 2); ctx.fill(); } } } start(); }
}
draw();
Canvas Optical Illusion - Script Codes
Canvas Optical Illusion - Script Codes
Home Page Home
Developer Kevin
Username KevinBruland
Uploaded August 27, 2022
Rating 3
Size 2,148 Kb
Views 26,312
Do you need developer help for Canvas Optical Illusion?

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!

Kevin (KevinBruland) 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!