Canvas Animation - SetInterval On Update

May 6, 2010

I'm new to Javascript, and having an issue with getInterval. For my programming languages class we have to create a simulation of waves breaking on a shore from above. I've decided to use a canvas object and Javascript for this animation, but am getting stuck on refreshing the frames.

Here is the code I have so far:
var shoreline = new Array();
var waves = new Array();
var canWidth = 600;
var canHeight = 400;
var waveNum = 0;
var count = 0; .....

As you can see, the idea is that each 5-pixel wide node of the shore and wave are stored in arrays so that the position of each node can differ. The wave() function initializes all the required data, and then calls setInterval on update(). The function update() draws the water, shore, and wave, and then updates the wave position node-by-node. Ideally, setInterval would cause this to happen repeatedly, so that the wave would appear to move closer to the shore every 1000ms.

However, this is not the case. It appears as though setInterval is working for one pass of update(), but fails after the first pass. You can tell that this is happening because the wave appears at its starting position, but does not continue to move. I read somewhere that setInterval can be broken by one-time functions, but I'm not sure. If you'd like to see what it's currently doing in a browser, I've hosted it here [URL]. Once I've got this animating properly I can continue my project, but I've hit a roadblock.

View 2 Replies


ADVERTISEMENT

SetInterval Animation

Mar 16, 2007

I want to use the setInterval function to animate a menu sliding down when the user clicks on a link. here's a snippet:

box.timer = window.setInterval(
function() {
box.heightCount++;
if(box.heightCount >= maxHeight) {
window.clearInterval(box.timer);
}
box.style.height = box.heightCount + 'px'

}, 1);

So this works ok in the sense that the animation is performed smoothly. But to my understanding it should expand the height of the box 1pixel every 1ms, so it should expand very quickly from the base height to the maxHeight limit, but I click on the link and it animates soooooo slooooowly -- smoothly, i mean, but it takes well over a minute to expand to the full height. I just need to speed the damn thing up! Also, if i change "box.heightCount++;" to "box.heightCount+=1;" it gets all messed up. What am I missing here?

View 1 Replies View Related

Using Translate To Move Canvas Around (Animation)

Jul 12, 2011

The main one I find is to use translate to move the canvas around, which just seems odd to me. I don't even fully understand how it works. But the one I was looking at used clearRect. In examples given looked as though it had to redraw the canvas every frame. I'm just mucking around with a simple poker game, figured I'd have the table in one layer and anything that moves on a top layer? By doing that I could only clear each card as it moved around...yes?

View 8 Replies View Related

HTML 5 Canvas Element Animation Save

May 18, 2010

My application is based on client-server architecture. It is a E-Learning Application. I am using ruby on rails server, javascript,canvas,SVG element. I have an idea of an interface to allow the teacher to build any type of animation for students using Cake javascript API. I wanted to save the animation build using the canvas element on the rails server either in database or as a file. Later I have to use that animation to be played to students. Any way to do it on any language except proprietary languages like flash but from that language communication can be done with rails server.

View 1 Replies View Related

Shim Layer For Bind() - Sprite Animation On The Canvas Wouldn't Work On Safari Or Older Versions Of Firefox

Aug 30, 2011

I have a script that runs a sprite animation on the canvas, adapted from here: [URL] After wondering why it wouldn't work on Safari or older versions of Firefox, I saw this: [URL] and implemented the suggested shim. However, I'm still getting some errors that I can't explain for the life of me. To make this easy for everyone, I threw it in a jsfiddle: [URL]

View 1 Replies View Related

SetInterval Of Function - Called From Function - Display And Update

Jan 28, 2011

I'm trying to make a time script for an app I'm making (this is not an HTML document; it's a .js file to be used with Titanium) however I'm having trouble getting the function to display and update.

function updateClock()
{
setInterval ( 'kiTime()', 1000 );
}
function kiTime ()
{
//Get current date and time
[Code]...

If I use kiTime() under the text field I will get the current time (or at least the time the app was opened), however if I call updateClock() it's blank.

View 4 Replies View Related

Delete Lines On Canvas, But Not The Whole Canvas?

Jul 12, 2011

I want to delete some lines on the canvas that i have created in HTML5 (with the <canvas> tag). I know one solution and that is using clearRect() (give this function the HEIGHT and WIDTH of the canvas and you clears the whole canvas.).But i don't want to clear the whole canvas, i only want to delete a few lines made by the function lineTo().

View 1 Replies View Related

CANVAS - Change Canvas Stroke On A Typeface Hover - Stroke() Doesn’t Work?

Mar 16, 2009

I have a menu in which is applied typeface script. Typeface 0.11 version doesn't support hover on links. There is an experimental version but doesn't work very well for me, so i tried to implement hover by myself.
Problem is: hover works pretty well changing canvas context property fillColor and then calling fill(), but the stroke on hovered voice remains on another color. Difficult to explain, so i put some test code online: TEST CODE As you can see, passing with mouse on the menu voices triggers the hover and the element change, but remains bordered in red.

[Code]...

View 5 Replies View Related

Get The SetInterval To Loop But When The SetInterval Is Called

Jan 5, 2010

I cant seem to get this to work.

I have this OO code and i want to get the setInterval to loop but when the setInterval is called, i get error saying that loop() is not define..

Code:
function setFunc(){
this.init = init;
function init(){

[Code]....

View 1 Replies View Related

JQuery :: Lightbox Animation Conflicts With Rollover Animation?

Jan 10, 2012

I have a button which is animated with a rollover effect, obtained through jQuery of course. I wish to open a lightbox clicking that button and I tried with a simple, self-made lightbox and with FancyBox but in either case the lightbox is displayed without animation.I post the code for my buttons and the lightboxHTML for buttons:

<ul> <li><div id="fader0"> <div class="from"><a class="products" href="#inline"><img src="images/tabproducts.png" alt="Our products" /></a></div> <div class="to"><a class="products" href="#inline"><img src="images/tabproductsHover.png" alt="Our products"

[code]....

View 1 Replies View Related

Make A Fade In Animation With SetTimout - Why Animation Only Subtract

Aug 27, 2011

I've been messing with this code to make a fade in animation with setTimout. The only thing is addition isn't working on the fade in. A subtraction on a negative works though. This seems strange to me.

changeit.style.opacity -= -0.01; works but when it's changed to changeit.style.opacity += 0.01; there's no fade in. It's the only thing I change. My intuition says to me it should work with addition, but maybe there's something I'm not understanding.

<html>

changeit.style.opacity -= -0.01; if this is set to changeit.style.opacity += 0.01; it doesn't work. What?

Of course this is all just for Firefox for now. If I put this in something useful I'll change it so it'll work in other browsers later.

View 8 Replies View Related

'this' And SetInterval

Nov 23, 2005

If I have some code that's a bit like this

Con = function() {
this.op = 1;
this.count = 0;
}
Con.prototype.loop = function() {
this.doNext = setInterval(this.next, 100);
}
Con.prototype.next = function() {
this.count++;
if (this.count > 10) clearInterval(this.doNext);
}

obj = new Con();

when I call obj.loop(); 'this' in obj.next refers to setInterval
and not obj . I realise I can add var ref = this; and send it with
setInterval but is there something I can do to get 'this' to refer to
obj when in obj.next?

View 3 Replies View Related

SetInterval() Help

Feb 8, 2006

I'm attempting to create a small function that when called upon will change the CSS background property 10 times at 80 millisecond intervals. I'm pretty sure setInterval() is the way to go about this, but I've been toying with my script for an hour and can't get anything to work.

At each 80 millisecond interval, the CSS background property should change like so:

background: url("image.png") 0 0 no-repeat;
background: url("image.png") 0 20px no-repeat;
background: url("image.png") 0 40px no-repeat;
background: url("image.png") 0 60px no-repeat;

and so on.

I would normally just use the background-position property, but it seems the hyphen throws javascript for a slip. It's really not necessary for this function, but if there's a way to use hyphens, it would be good to know for future reference.

View 7 Replies View Related

Catching Keypress In Canvas Tag

Jun 22, 2006

I am trying to catch keypresses in my canvas tag, but it appears that
that is not valid.

I may try to just create a hidden input box that will allow me to catch
keypresses.

I am trying to write a very simple asteroids game for a presentation on
Ajax in July, to show that javascript can do more than it was able to a
couple of years ago.

View 3 Replies View Related

JQuery :: Add A Class To Canvas?

Aug 16, 2010

I am working with the new canvas element. I have simplified my request.

Can I add a class to the canvas JS? Currently the fill colour is set with 'context.fillStyle="red";' I would love have this in a class as I want to change the colour of the fill with jQuery.

<!DOCTYPE html><html><head><meta charset=utf-8"><title>Canvas</title></head><body>
<canvas id="myCanvas"></canvas>
<script type="text/javascript">
var canvas=document.getElementById('myCanvas');

[Code]....

View 2 Replies View Related

Canvas Doesnot Work In IE?

Feb 23, 2010

Why doesn�t the following code work in IE, I use excanvas.js as told in other articles? excanvas is in the same dir as the html-file.

Code:

<html>
<head>
<title></title>[code].....

View 1 Replies View Related

Why Won't Canvas Graphics Get Rendered

Nov 3, 2011

Why won't my canvas graphics get rendered?

[Code]...

View 3 Replies View Related

Multiple Layers With The Canvas Tag?

Feb 13, 2011

I'm writing a script that graphs data in different colors and shapes and I'm encountering two problems:(1) I can't specify different colors for the graph I'm rendering. If I use fillStyle="red"; then the entire chart becomes red. It seems to use the last color specified as the color for everything on the chart.(2) The shapes are overlapping in strange ways. The fills are not covering the lines of shapes behind them so it's unclear which shape is in front and which is behind. It's like they aren't being treated as separate shapes but instead one shape where boundaries are unclear. I don't know how else to explain it.

View 2 Replies View Related

Can't Get HTML Canvas To Work In IE

Jan 1, 2011

I have make a javascript program that uses HTML canvas and I can't get it to work in Internet Explorer. I tried using excanvas and jqcanvas but had trouble getting them to work.

View 5 Replies View Related

Mozilla And SetInterval

Jul 20, 2005

Is some future Mozilla going to support setInterval ( <function:function>,
<interval:number> ) ?

Right now it seems to be simply setInterval ( <function-text:string>,
<interval:number> )

View 5 Replies View Related

Use SetInterval On A Loop?

May 4, 2011

How would you use setInterval so that theres a delay each time javascript runs through a certain loop?

View 10 Replies View Related

Using SetInterval In Loops

Dec 15, 2007

i'm scratching my head over achieving similar results with setInterval() function, and how I can keep it from looping infinitely.

I want to do something like this:
var i = 0;
var endTime = now + ((1000*60)*2); // 2 minutes after now
while (now <=endTime) {
i = i + 1;
now = new Date().getTime();
}
document.write("total iterations: " + i);

however you can't do this because of lag issues, so i'll settle for using setinterval on its smallest interval of a millisecond, here is my attempt to translate the above to a setinterval solution:
var endTime = now + ((1000*60)*2); // 2 minutes after now
var intervalID = setInterval(loopFunc(endTime),1);
function loopFunc(endTime,intervalID) {
if (new Date().getTime() <= endTime) {
i = i + 1;
} else {
clearInterval(intervalID);
}}

as you can see I have prolbems figuring out how to stop the interval from continuing to iterate, and passing the interval id, I'm clueless Also I'm clueless on echoing the total iterations via this method.

View 4 Replies View Related

SetInterval / SetTimeout

Sep 3, 2005

I'm trying to build the framework for an AJAX back/forward button fixer. Basically it uses the hash fix for them (using text after the # to add history objects). To this effect, the function to check for updates has to be run every second or so. No problem, I'll just use setInterval for that.

For whatever reason, though, setInterval gives me an error. Specifically that oldLocation (global variable) is undefined (in the checkURL function). So I try setTimeout. Same thing. When I just call the function explicitly once (adding checkURL(); to the end of the script) everything works wonderfuly; it's just setting it up on a timer that messes up. Frankly, I've no idea why it's doing this. (The bug just seems so odd) Maybe a more advanced JS coder can shed some light on it.. Code:

View 6 Replies View Related

Using Output For HTML Elements Outside Canvas

Apr 7, 2009

Before the canvas tag you had to write a script to make a gradient for html element backgrounds. Now you can use canvas and it's as easy as ... well messing with canvas. I have this script. At one point I had the gradient img objects resizing so the gradient would not get skewed. it was simple. just set the height% at the opposite ratio of the canvas width/height. But I went to make some changes and the next thing i knew that no longer worked. I don't remember how I did it!! I'm going to have to start over if I can't figure it out. Here's the script. Canvas only.

I haven't adapted with excanvas for IE yet, but that's coming when I can figure out how I had the thing working right. It's a css issue. I had thought I put the img objects in divs that were sized and positioned and then set the images to width 100% height= 100%*((canvas.width-canvas.height)/canvas.width) So in my example, the canvas was 100 x 30 and the factor is 7 so it would resize the image 700% and the height would grow by the relative amount to the setting I had it. I'll try to figure it out, but there's the script working somewhat. I want to figure out a way to resize the img objects when the user resizes the page, but I want the img to always maintain aspect ratio. [URL]

<script>
/*ORIGINAL SCRIPT FOUND AT [URL]*/
window.onload = function () {
var canvas = document.getElementById('c');
var ctx = canvas.getContext('2d');
var g = ctx.createLinearGradient(0, 0, 100, 50);
g.addColorStop(0, 'rgba(255, 255, 0, 0.5)');
g.addColorStop(0.5, '#0ff');
g.addColorStop(1, '#00f');
ctx.fillStyle = g;
ctx.fillRect(0, 0, 100, 50);
var data = canvas.toDataURL();
document.getElementById('i').src = data;
document.getElementById('EIMG').src = data;

var canvas2 = document.getElementById('c2');
var ctx2 = canvas2.getContext('2d');
var g2 = ctx2.createLinearGradient(0, 0, 100, 50);
g2.addColorStop(0, 'rgba(255, 0, 255, 0.5)');
g2.addColorStop(0.5, '#0ff');
g2.addColorStop(1, '#00f');
ctx2.fillStyle = g2;
ctx2.fillRect(0, 0, 100, 50);
var data2 = canvas2.toDataURL();
document.getElementById('i2').src = data2;
document.getElementById('DIMG').src = data2; .....

View 3 Replies View Related

Drawing On Multiple Canvas Elements?

Oct 21, 2009

I'm having a problem with the <CANVAS> element in JavaScript. here's what i'm trying to do: I need 3 different canvas elements for writing a signature to. they must all 3 be visible at the same time

My problem that i'm having is that i can't seem to get the code to work which will write to more than one canvas.

[Code]...

View 2 Replies View Related

Creating Canvas With Document.CreateElement?

Feb 25, 2010

Why doesn't the following code work:
<script type='text/javascript'>
var canvas = document.createElement('canvas');
canvas.width = 1400;
canvas.height = 700;
var context = canvas.getContext('2d');
context.strokeRect(10, 10, 50, 50);
context.stroke();
</script>

View 4 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved