javascript - Is there any way to draw a "streak" (fading "digital phosphor" effect) on HTML5 canvas? -


i want draw moving dot on html5 canvas follows complicated trajectory. know how do; see, example, lorenz attractor implemented below. small dots hard follow. is there way add blurry trail behind dot? can keep past history of drawn points, don't know how make them fade.

in technical terms, suppose polyline/curve opacity/width/color changes smoothly along curve. know how draw polylines (and can figure out bezier curve stuff if need to), don't know how apply smooth gradient along path.

(digital oscilloscopes solved "problem" having digital phosphor oscilloscope effect scope emulated old analog "phosphor" effect: areas hit scope's "beam" take while fade.)

<!doctype html>  <html>  <head><script type="text/javascript">    document.addeventlistener("domcontentloaded", function(event) {  	var x = 1, y = 0, z = 0, t=0;  	function ontick(timestamp)  	{  		var ctx = document.getelementbyid('canvas').getcontext('2d');  		ctx.clearrect(0, 0, 300, 300);  		ctx.fillstyle = 'black';  		var cx = 150;  		var cy = 150;  		var r = 5;  		var = timestamp * 0.001;  		var dt = - t;  		t = now;  		if (dt > 0.1)  			dt = 0.1;  		// lorenz attractor  		var sigma = 10, rho=28, beta=8/3;  		var dxdt = sigma*(y-x);  		var dydt = x*(rho-z)-y;  		var dzdt = x*y-beta*z;  		x += dt*dxdt;  		y += dt*dydt;  		z += dt*dzdt;  		  		var drawx = cx + r*x;  		var drawy = cy + r*y;  		var rdot = 2;  		ctx.beginpath();  		ctx.arc(drawx, drawy, rdot, 0, 2 * math.pi, true);  		ctx.fill();  		requestanimationframe(ontick);		  	}  	requestanimationframe(ontick);    });  </script></head>  <body>    <canvas id="canvas" width="300" height="300"/>    </body>  </html>

instead of clearing rectangle each frame, paint in alpha channel save previous dots momentarily. replaced clearrect fillrect fillstyle white see-through.

keep in ming can adjust alpha channel, make dot stay longer/short duration. in code 0.04 in ctx.fillstyle = "rgba(255, 255, 255, 0.04)";. adjusted lower make traces stay longer time.

document.addeventlistener("domcontentloaded", function(event) {  	var x = 1, y = 0, z = 0, t=0;  	function ontick(timestamp)  	{  		var ctx = document.getelementbyid('canvas').getcontext('2d');  		//ctx.clearrect(0, 0, 300, 300);          ctx.fillstyle = "rgba(255, 255, 255, 0.04)";          ctx.fillrect(0, 0, 300, 300);  		ctx.fillstyle = 'black';  		var cx = 150;  		var cy = 150;  		var r = 5;  		var = timestamp * 0.001;  		var dt = - t;  		t = now;  		if (dt > 0.1)  			dt = 0.1;  		// lorenz attractor  		var sigma = 10, rho=28, beta=8/3;  		var dxdt = sigma*(y-x);  		var dydt = x*(rho-z)-y;  		var dzdt = x*y-beta*z;  		x += dt*dxdt;  		y += dt*dydt;  		z += dt*dzdt;  		  		var drawx = cx + r*x;  		var drawy = cy + r*y;  		var rdot = 2;  		ctx.beginpath();  		ctx.arc(drawx, drawy, rdot, 0, 2 * math.pi, true);  		ctx.fill();  		requestanimationframe(ontick);		  	}  	requestanimationframe(ontick);    });
canvas {    position: absolute;    width: 100%;    height: 100%;    top: 0;    left: 0;    z-index: -1;  }
<canvas id="canvas"></canvas>


Comments

Popular posts from this blog

javascript - Clear button on addentry page doesn't work -

c# - Selenium Authentication Popup preventing driver close or quit -

tensorflow when input_data MNIST_data , zlib.error: Error -3 while decompressing: invalid block type -