Moderators: gmalivuk, Moderators General, Prelates
cjmcjmcjmcjm wrote:If it can't be done in an 80x24 terminal, it's not worth doing
cjmcjmcjmcjm wrote:If it can't be done in an 80x24 terminal, it's not worth doing
Well no, this isn't going to be an actual wave, it's going to be an particle object travelling along it. More specifically quite a number of them with varying amplitude, etc as part of an effect. I was thinking of doing semi-circles, but the effect isn't quite what I wanted. I might try it with varying length of circular arcs to see what it looks like.Meem1029 wrote:For computer graphics, I would recommend seeing if you could cheat and use sections of a circle to represent the sine curve.
ameretrifle wrote:Magic space feudalism is therefore a viable idea.
<!DOCTYPE HTML PUBLIC>
<html>
<head>
<title>Sine Anim</title>
<meta http-equiv="Content-Script-Type" content="text/javascript">
<style type="text/css">
H3 { text-align: center; font-size: large; color: #9944aa; }
canvas { position:relative; }
#ocanvas { border: 1px solid black; }
#candiv {text-align: center; }
</style>
<script>
var canvas, ctx, scanvas, params,
width, height, Pi2 = 2 * Math.PI,
framerate = 16, //how often to draw a new frame, in milliseconds
frequency = 2, amplitude = 0.8, //sine wave parameters relative to canvas size
delta = 1, //sprite speed
spriterad = 10, //sprite size
spritepos = 0, spriteX = 0, spriteY = 0,
count = 0, lastTime = 0, go = false;
function ById(id){return document.getElementById(id)}
//Create a canvas element
function make_canvas(w, h)
{
var c = document.createElement('canvas');
c.width = w;
c.height = h;
return c;
}
/*
A requestAnimationFrame selector with setTimeout fallback,
by Paul Irish, with improved fallback code by Erik Moller. See
http://paulirish.com/2011/requestanimationframe-for-smart-animating/
http://my.opera.com/emoller/blog/2011/12/20/requestanimationframe-for-smart-er-animating
requestAnimationFrame calls the callback with the current time.
*/
window.requestAnimFrame = (function(){
var lastTime = 0;
return window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.oRequestAnimationFrame ||
window.msRequestAnimationFrame ||
function (callback)
{
var currTime = + new Date();
var timeToCall = Math.max(0, 16 - (currTime - lastTime));
window.setTimeout(function()
{callback(currTime + timeToCall);}, timeToCall);
lastTime = currTime + timeToCall;
}
})();
//The animation callback. The parameter is the current time.
function animate(time)
{
count += 1;
if (!go)
return;
// Request the next animation frame
window.requestAnimFrame(animate);
if(time - lastTime > framerate)
{
lastTime = time;
spritepos = spritepos + delta;
draw();
}
};
function show_params()
{
params.innerHTML =
'go=' + go + '<br>' +
'spritepos=' + spritepos + '<br>' +
'count=' + count + '<br>' +
'lastTime=' + lastTime;
}
function ev_mousedown(ev)
{
go = !go;
if (go)
animate(+ new Date());
else
show_params();
}
//Move sprite with Approximate constant speed along sine curve
function draw()
{
//Compute derivative at current point
var slope = amplitude * frequency * Math.cos(spriteX * frequency);
//Compute change in x using Pythagorean relation ds^2 = dx^2 + dy^2,
// where ds is an element of arc length
spriteX += delta / Math.sqrt(1 + slope * slope);
if (spriteX >= width)
spritepos = spriteX = 0;
spriteY = amplitude * Math.sin(spriteX * frequency);
put_sprite();
show_params();
}
function put_sprite()
{
scanvas.style.left = spriteX + scanvas.ox + 'px';
//Make +Y up
scanvas.style.top = -spriteY + scanvas.oy + 'px';
}
function draw_sine(canvas)
{
var ctx = canvas.getContext('2d'), x, y;
ctx.beginPath();
ctx.moveTo(0, 0);
for (x=0; x<=width; x++)
{
y = amplitude * Math.sin(x * frequency);
ctx.lineTo(x, y);
}
ctx.closePath();
ctx.stroke();
}
function setup()
{
canvas = ById('ocanvas');
if (canvas.getContext)
{
ctx = canvas.getContext('2d');
//Get window dimensions & calculate canvas dimensions
height = canvas.height = Math.floor(0.75 * window.innerHeight);
width = canvas.width = Math.floor(0.75 * window.innerWidth);
//Put origin at centre left
ctx.translate(0, height / 2);
//Make +Y up
ctx.scale(1, -1);
ctx.strokeStyle = '#000';
//Draw sine wave path
frequency *= Pi2 / width;
amplitude *= height * 0.5;
draw_sine(canvas, frequency, amplitude);
//Set up sprite
scanvas = make_canvas(2*spriterad, 2*spriterad);
ById('candiv').appendChild(scanvas);
ctx = scanvas.getContext('2d');
//Set positioning origin for sprite, allowing for canvas border.
scanvas.ox = -(canvas.width + 6 + spriterad);
scanvas.oy = -(0.5 * canvas.height + 1 - spriterad);
//Put drawing origin in centre
ctx.translate(spriterad, spriterad);
ctx.fillStyle ="hsla(" + 0 + ",100%, 50%, 0.5)";
ctx.strokeStyle ="hsla(" + 240 + ",100%, 50%, 0.75)";
//Draw sprite
ctx.beginPath();
ctx.arc(0,0, spriterad, 0, Pi2, false);
ctx.fill();
//ctx.stroke();
params = ById('paramsdiv');
//do initial frame
draw();
canvas.addEventListener('mousedown', ev_mousedown, false);
}
else alert("Sorry, I can't set up the canvas!");
}
</script>
</head>
<body onload="setup();">
<h3>Sine Animation</h3>
<div id=candiv>
<canvas id="ocanvas">
If you can read this, your browser does not support the HTML5 Canvas.
</canvas>
</div>
<p>
<div style="font-weight:bold">Animation parameters</div>
<div id="paramsdiv"></div>
<p>
Sinusoidal motion demo. The particle moves along the sine wave with (approximately) constant speed.
<br>
</body>
</html>
elminster wrote:Wow, that's great. Thanks for the time to even prepare a sample.
Didn't take much to work to get a rotated sine wave working (Probably non-optimal way), but wasn't consistent speed.
Were working off a really old DirectDraw (DirectX 7.0a) system, which frankly does barely anything useful, not even blending. Porting it is obviously not a small task.
Users browsing this forum: Bakstoola, Google [Bot], Parralelex and 1 guest