admin管理员组

文章数量:1292101

Here I have a very simple script.

stage = new createjs.Stage("myCanvas");

circle = new createjs.Shape();
circle.graphics.beginFill("red").drawCircle(0,0,40);

And my HTML

<!DOCTYPE HTML>

<html>
<head>

<link rel="stylesheet" type="text/css" href="mainStyle.css">
<script src="mainScript.js"></script>
<script src="create.js"></script>

</head>
<body>  

    <canvas id="myCanvas" width=1000 height=500 ></canvas>

</body>
</html>

Yet it does not work. I get this error:

"createjs is not defined"

But in my HTML it is linked to file. Is there anything else that could be causing this?

Here I have a very simple script.

stage = new createjs.Stage("myCanvas");

circle = new createjs.Shape();
circle.graphics.beginFill("red").drawCircle(0,0,40);

And my HTML

<!DOCTYPE HTML>

<html>
<head>

<link rel="stylesheet" type="text/css" href="mainStyle.css">
<script src="mainScript.js"></script>
<script src="create.js"></script>

</head>
<body>  

    <canvas id="myCanvas" width=1000 height=500 ></canvas>

</body>
</html>

Yet it does not work. I get this error:

"createjs is not defined"

But in my HTML it is linked to file. Is there anything else that could be causing this?

Share Improve this question asked Feb 22, 2013 at 3:54 starscapestarscape 2,9935 gold badges30 silver badges40 bronze badges 2
  • can you post the code for linked to that file? – Iswanto San Commented Feb 22, 2013 at 3:56
  • Modified it to include the HTML – starscape Commented Feb 22, 2013 at 3:58
Add a ment  | 

3 Answers 3

Reset to default 5
<script src="create.js"></script>
<script src="mainScript.js"></script>

Your code needs to execute AFTER including create.js - order matters.

There are a couple problems with your code.

First, and to directly answer the question, you have to load the library which constructs the createjs object before you load your script which instantiates it. Change the order of the script tags you have in your head.

Second, your instantiation will execute before the canvas element is added to the DOM. To fix this you need to listen for the document's onload event. Then execute your code.

Third, to get the red circle object to actually show up you need to append it to the canvas and then call the stages update method.

Ex:

window.onload = function(){
    stage = new createjs.Stage("myCanvas");

    circle = new createjs.Shape();
    circle.graphics.beginFill("red").drawCircle(0,0,40);

    stage.addChild(circle);

    stage.update();
}

For angular projects the above answers might not be sufficient to solve the problem. What I did:

myAngularModule.service('preloadservice', function ($rootScope) {

  if (typeof createjs === 'undefined') {
    setTimeout(continueAfterWait, 500);
  } else {
    continueAfterWait();
  }

  var continueAfterWait = function () {
    var queue = new createjs.LoadQueue();
    //...
  } 
}

本文标签: javascriptI get the error quotcreatejs is not definedquot even though I linked to the fileStack Overflow