Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
<!DOCTYPE html>
<html lang="en">
<head runat="server">
<title></title>
<meta http-equiv="X-UA-Compatible" content="IE=Edge"//>
<script type="text/javascript">
var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");
var grd = ctx.createLinearGradient(0, 0, 175, 50);
grd.addColorStop(0, "#FF0000");
grd.addColorStop(1, "#00FF00");
ctx.fillStyle = grd;
ctx.fillRect(0, 0, 175, 50);
</script>
</head>
<body>
<form id="form1" runat="server">

<canvas id="myCanvas" width="200" height="100"
style="border:1px solid #000000;">
</canvas>
</form>
</body>
</html>
what else I need to do in this? I'm using 2010 where I install SP1.HTML5 intellisense is coming on page but not in JS.
Posted 17-Jan-13 20:44pm
Edited 17-Jan-13 20:48pm
v2

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Your script is in the header where it will be run as soon as it is loaded - which is before the canvas element has been added to the document.
 
You need to wait until the document has loaded before trying to use getElementById(). The easiest way is to wrap your code in an onload handler:
window.onload = function() {
  var c = document.getElementById("myCanvas");
  ...
  ... // more code here...
}
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Zoltán Zörgő 380
1 Sergey Alexandrovich Kryukov 185
2 OriginalGriff 150
3 Mika Wendelius 83
4 Peter Leow 80
0 Sergey Alexandrovich Kryukov 8,373
1 OriginalGriff 6,466
2 Peter Leow 3,567
3 Zoltán Zörgő 3,316
4 Richard MacCutchan 2,397


Advertise | Privacy | Mobile
Web01 | 2.8.150123.1 | Last Updated 18 Jan 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100