Skip to content

Instantly share code, notes, and snippets.

@Skoatpalace
Created August 25, 2020 12:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Skoatpalace/1b01825e363ece7de0525f451f74f53d to your computer and use it in GitHub Desktop.
Save Skoatpalace/1b01825e363ece7de0525f451f74f53d to your computer and use it in GitHub Desktop.
Canvas HTML Sinusoïdal
const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
canvas.addEventListener('resize', () => {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
})
const color = {
h: 360,
s: 50,
l: 50
}
const vague = {
y: canvas.height / 2,
nbCourbes: 0.01,
amplitude: 300,
frequence: 0.01
}
let increment = vague.frequence
const animate = () => {
requestAnimationFrame(animate);
ctx.fillStyle = 'rgba(0, 0, 0, 0.01)';
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.beginPath();
ctx.moveTo(0, vague.y);
for (let i = 0; i < canvas.width; i++) {
ctx.lineTo(i, vague.y + Math.sin(i * vague.nbCourbes + increment) * vague.amplitude * Math.sin(increment));
}
ctx.strokeStyle = `hsl(${Math.abs(color.h) * Math.sin(increment)}, ${color.s}%, ${color.l}%)`
ctx.stroke();
increment += vague.frequence;
}
animate();
<!DOCTYPE html>
<html lang="fr">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Sinusoïdal canvas</title>
<style>
* {
margin: 0;
padding: 0;
}
html, body {
width: 100%;
height: 100%;
}
canvas {
display: block;
}
</style>
</head>
<body>
<canvas id="canvas"></canvas>
<script src="app.js"></script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment