/    Sign up×
Community /Pin to ProfileBookmark

Basic question (element attributes & function invocation)

The following code make a square move left to right, back and forth:

[CODE]<!DOCTYPE HTML>
<html>
<head>
<style>
body {
margin: 0px;
padding: 0px;
}
</style>
</head>
<body onLoad=”animate()”>
<canvas id=”canvas” width=”800″ height=”800″></canvas>
<script>
var canvas = document.getElementById(‘canvas’);

var context = canvas.getContext(‘2d’);

var X = 0;

var Y = 10;

var goRight = true;

var Draw = {

rectangle: function (x, y) {

context.beginPath();

context.rect(x, y, 10, 10);

context.fillStyle = ‘#8ED6FF’;

context.fill();

context.lineWidth = 5;

context.strokeStyle = ‘black’;

context.stroke();
}
}

function animate() {

if (X < 800 && goRight) {

X += 10;

} else if (X > 0 || !goRight) {

X -= 10;
goRight = (X <= 0); // reset direction once we hit 0;

}

context.clearRect(0, 0, canvas.width, canvas.height);

Draw.rectangle(X, Y);
window.requestAnimationFrame(animate);

}
</script>
<button onclick=”animate()”>test</button>

</body>
</html>

[/CODE]

However, when I remove the ‘onLoad’ attribute from the body element, I thought that clicking the ‘Test’ button should make the rectangle animate in the same way, but nothing happens. Why is this?

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@cootheadAug 21.2015 — Hi there Zukias,

[indent]

this...
[color=navy]
&lt;!DOCTYPE HTML&gt;
&lt;html lang="en"&gt;
&lt;head&gt;

&lt;meta charset="utf-8"&gt;
&lt;meta name="viewport" content="width=device-width, initial-scale=1.0"&gt;

&lt;title&gt;untitled document&lt;/title&gt;

&lt;style media="screen"&gt;
body {
margin:0;
}
#but{
display:block;
}
#canvas {
display:block;
}
p {
width:800px;
}
&lt;/style&gt;

&lt;/head&gt;
&lt;body&gt;

&lt;button id="but"&gt;start&lt;/button&gt;

&lt;canvas id="canvas" width="800" height="30"&gt;&lt;/canvas&gt;

&lt;p&gt;
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Donec ultricies sollicitudin nisi, ut molesti.
&lt;/p&gt;

&lt;script&gt;
(function() {
'use strict';
var b=document.getElementById('but'),test=true,wraf;

b.onclick=function() {
test===true?
(window.requestAnimationFrame(animate),test=false,b.innerHTML='stop'):
(window.cancelAnimationFrame(wraf),test=true,b.innerHTML='start');
}

var canvas=document.getElementById('canvas');
var context=canvas.getContext('2d');
var X=0;
var Y=10;
var goRight=true;
var Draw={
rectangle:function (x,y) {
context.beginPath();
context.rect(x,y,10,10);
context.fillStyle='#8ed6ff';
context.fill();
context.lineWidth=5;
context.strokeStyle='#000';
context.stroke();
}
}

function animate() {

if((X&lt;800)&amp;&amp;(goRight)) {
X+=10;
}
else {
if((X&gt;0)||(!goRight)) {
X-=10;
goRight=(X&lt;=0); // reset direction once we hit 0;
}
}
context.clearRect(0,0,canvas.width,canvas.height);
Draw.rectangle(X,Y);
wraf=window.requestAnimationFrame(animate);
}
})();
&lt;/script&gt;
&lt;/body&gt;
&lt;/html&gt;[/color]

...works OK for me in [i]"Firefox 40.0.2"[/i], [i]"Chrome 44.0.2"[/i], [i]"IE11"[/i] and [i]"Opera 31.0"[/i]. ?

[/indent]

[i]coothead[/i]
×

Success!

Help @Zukias spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.13,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...