admin管理员组

文章数量:1404927

I tried to create an SimpleImage using JavaScript. But it gives me a console error as

Uncaught ReferenceError: SimpleImage is not defined

I am unsure where the issue is ing from with the code below:

<script>
var img = new SimpleImage(200, 200);
for (var p of img.values()){
    x = p.getX();
    y = p.getY();
    w = img.getWidth()
    if (x > y){
    p.setRed(255);
    }
    if (x + y > w)
    {
        p.setRed(255);
    }
    if (x > 20){
        p.setRed(255);
    }
   }
print (img);
</script>

I tried to create an SimpleImage using JavaScript. But it gives me a console error as

Uncaught ReferenceError: SimpleImage is not defined

I am unsure where the issue is ing from with the code below:

<script>
var img = new SimpleImage(200, 200);
for (var p of img.values()){
    x = p.getX();
    y = p.getY();
    w = img.getWidth()
    if (x > y){
    p.setRed(255);
    }
    if (x + y > w)
    {
        p.setRed(255);
    }
    if (x > 20){
        p.setRed(255);
    }
   }
print (img);
</script>
Share Improve this question edited Jan 25, 2018 at 14:26 sebasp 113 bronze badges asked Jan 25, 2018 at 14:15 Rajind PamodaRajind Pamoda 1432 silver badges10 bronze badges 5
  • 4 By SimpleImage you mean the library? If so, check if you're importing it correctly. Also, the SimpleImage constructor needs a Canvas and not width/height. – Fellipe Augusto da Silva Commented Jan 25, 2018 at 14:17
  • I want to create a simple image using javascript.. How to include a library to my script? – Rajind Pamoda Commented Jan 25, 2018 at 14:25
  • Please elaborate. What do you mean by "simple image"? – Fellipe Augusto da Silva Commented Jan 25, 2018 at 14:27
  • I'm following this code here sites.google./site/tech23repo/scripting/javascript – Rajind Pamoda Commented Jan 25, 2018 at 14:28
  • 2 @RajindPamoda, imo this site is for the trash bin. Because the code doesn't work without a library AND they don't mention what library they use AND the one library I found doesn't provide the functions they use. So I don't know how to make their code work – Thomas Commented Jan 25, 2018 at 14:48
Add a ment  | 

3 Answers 3

Reset to default 2

The problem here is that you first need to include a library that contains SimpleImage() function, JavaScript doesn't include a predefined library like the one you mention. In the webpage you are looking for (sites.google./site/tech23repo/scripting/javascript), it also doesn't show which library you should be including in your script. So for example, for this to work:

<script>
   var img = new SimpleImage(200, 200);
   for (var p of img.values()){
       x = p.getX();
       y = p.getY();
       w = img.getWidth()
       if (x > y){
           p.setRed(255);
       }
       if (x + y > w){ //Indent this correctly
           p.setRed(255);
       }
       if (x > 20){
           p.setRed(255);
       }
   }
   print (img);
</script>

You need to have defined the method SimpleImage() somewhere, something like this:

<script async="" src="https://www.dukelearntoprogram./course1/mon/js/image/SimpleImage.js"></script>
<script>
    //Rest of the code using SimpleImage() method goes here.
   var img = new SimpleImage(200, 200);
   for (var p of img.values()){
       x = p.getX();
       y = p.getY();
       w = img.getWidth()
       if (x > y){
           p.setRed(255);
       }
       if (x + y > w){ //Indent this correctly
           p.setRed(255);
       }
       if (x > 20){
           p.setRed(255);
       }
   }
   print (img);
</script>

The library you require is from the Duke / Coursera course you can obtain it here. http://www.dukelearntoprogram./course1/mon/js/cs101/SimpleImage.js

You can find more info here http://www.dukelearntoprogram./course1/

You require the library of DLTP(Duke University CourseEra Course).You can Inject it into your code offline or by online. Online Injection of SimpleImage library:

<script src="https://www.dukelearntoprogram./course1/mon/js/image/SimpleImage.js" >

Offline Injection of SimpleImage library download link[DLTP Library Download Link]::https://www.dukelearntoprogram.//downloads/archives/cs101.zip

本文标签: imageCreate a SimpleImage on JavaScriptStack Overflow