Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
164
rated 0 times [  165] [ 1]  / answers: 1 / hits: 117008  / 14 Years ago, thu, september 9, 2010, 12:00:00

For example, can I take this script (from mozilla tutorial):



<html>
<head>
<script type=application/javascript>
function draw() {
var canvas = document.getElementById(canvas);
if (canvas.getContext) {
var ctx = canvas.getContext(2d);

ctx.fillStyle = rgb(200,0,0);
ctx.fillRect (10, 10, 55, 50);

ctx.fillStyle = rgba(0, 0, 200, 0.5);
ctx.fillRect (30, 30, 55, 50);
}
}
</script>
</head>
<body onload=draw();>
<canvas id=canvas width=150 height=150></canvas>
</body>
</html>


and mix this JavaScript with jQuery's document.ready instead of relying on onload?


More From » jquery

 Answers
25

Yes, they're both JavaScript, you can use whichever functions are appropriate for the situation.



In this case you can just put the code in a document.ready handler, like this:



$(function() {
var canvas = document.getElementById(canvas);
if (canvas.getContext) {
var ctx = canvas.getContext(2d);

ctx.fillStyle = rgb(200,0,0);
ctx.fillRect (10, 10, 55, 50);

ctx.fillStyle = rgba(0, 0, 200, 0.5);
ctx.fillRect (30, 30, 55, 50);
}
});

[#95671] Sunday, September 5, 2010, 14 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
janjadonb

Total Points: 4
Total Questions: 114
Total Answers: 118

Location: Mali
Member since Fri, Dec 3, 2021
3 Years ago
janjadonb questions
;