Click here to Skip to main content
15,747,059 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
<!DOCTYPE html>
<html lang="en">
<head runat="server">
<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);
<form id="form1" runat="server">

<canvas id="myCanvas" width="200" height="100"
style="border:1px solid #000000;">

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.
Updated 17-Jan-13 19:48pm

1 solution

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...
Share this answer

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900