Thursday, October 3, 2024
HomeJavaScriptOptical phantasm - straightforward JavaScript tutorial

Optical phantasm – straightforward JavaScript tutorial


This is a cool optical phantasm – it’s best to see little white dots within the intersections of the grey strains the place you focus your imaginative and prescient, however they appear to vanish out of your peripheral imaginative and prescient. Please observe that this can be a static picture – it doesn’t change, however your mind thinks it does.

The code to create this impact could be very easy:

[Lines 4-6]: arrange the HTML5 Canvas

[7-9]: put together to attract the grey strains

[10-12]: draw the strains going South-West

[13-16]: draw the strains going South-East

[18-24]: draw the white circles. In each iteration of the y loop we draw two circles – the second is to shifted down and proper, in order that we cowl every intersection.

<html>
<physique fashion=’backgroundcoloration:black‘>
<canvas id=”myCanvas” width=”800” top=”340“></canvas>
<script>
let canvas = doc.getElementById(‘myCanvas‘);
let context = canvas.getContext(‘second’);
context.lineWidth = 5;
context.strokeStyle = ‘grey
context.beginPath();
for(let x=0;x<28;x++){
        context.moveTo(x*40304,0);
        context.lineTo(x*40+20,340);
        context.moveTo(x*40,0);
        context.lineTo(x*40324,340);
        }
context.stroke();
 
context.fillStyle = ‘white‘;
for(let x=0;x<20;x++)
        for(let y=0;y<9;y++){
                context.beginPath();
                context.arc(8+x*40y*429302 * Math.PI);
                context.arc(28+x*40y*4229302 * Math.PI);
                context.fill();                        
                }
</script>
</physique>
</html>

Altering the scale and decreasing the variety of circles result in fascinating results. Get pleasure from fooling your mind!

Take a look at these programming tutorials:

JavaScript:

Oldschool hearth impact (20 strains)

Animated fractal (32 strains)

8-bit sine scroll (30 strains)

Physics engine for newcomers

Starfield (21 strains)

Yin Yang with a twist (4 circles and 20 strains)

Tutorial – interactive, animated sprites

Your first program in JavaScript: you want 5 minutes and a notepad

Fractals in Excel

Python in Blender 3d:

Domino impact (10 strains)

Wrecking ball impact (14 strains)

3d fractal in Blender Python


RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments