<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom">
	<channel>
      <title>Tagged with preload() - Processing 2.x and 3.x Forum</title>
      <link>https://forum.processing.org/two/discussions/tagged/feed.rss?Tag=preload%28%29</link>
      <pubDate>Sun, 08 Aug 2021 17:13:59 +0000</pubDate>
         <description>Tagged with preload() - Processing 2.x and 3.x Forum</description>
   <language>en-CA</language>
   <atom:link href="/two/discussions/taggedpreload%28%29/feed.rss" rel="self" type="application/rss+xml" />
   <item>
      <title>How can I clone an image object?</title>
      <link>https://forum.processing.org/two/discussion/18109/how-can-i-clone-an-image-object</link>
      <pubDate>Thu, 08 Sep 2016 06:02:29 +0000</pubDate>
      <dc:creator>thinsoldier</dc:creator>
      <guid isPermaLink="false">18109@/two/discussions</guid>
      <description><![CDATA[<p>I want to load an image and draw it once with a mask and again without the mask.</p>

<p>I though I could load it then duplicate it as img2 and only apply the mask to the first img object.</p>

<p>Is that possible or is there a different way to do it?</p>

<pre><code>function preload() {
  img = loadImage("assets/moonwalk.jpg");
  imgMask = loadImage("assets/mask.png");
  img2 = ? ? ? ? ? ?; // how do i clone the first img?
}

function setup() {
  createCanvas(720, 400);
  img.mask(imgMask);
  imageMode(CENTER);
}

function draw() {
  background(250, 255, 3);
  image(img2, width/2, height/2);
  image(img, mouseX, mouseY);
}
</code></pre>
]]></description>
   </item>
   <item>
      <title>modifying the color of a png image</title>
      <link>https://forum.processing.org/two/discussion/27864/modifying-the-color-of-a-png-image</link>
      <pubDate>Fri, 27 Apr 2018 16:15:51 +0000</pubDate>
      <dc:creator>sillyfrog</dc:creator>
      <guid isPermaLink="false">27864@/two/discussions</guid>
      <description><![CDATA[<p>Hello everybody,</p>

<p>I am at a loss trying to change the color of a png image. Nothing I have tried seems to be working.
Here is my code so far (the image (note.png is just a black music eight on a transparent background which you can easily recreate). I want the black color of the note to change, not the background.</p>

<pre><code>var img;

function setup() {
  createCanvas(100, 100);
  pixelDensity(1);
  img = loadImage("../data/note.png");
}

function draw() {
  background(255, 0, 0, 50);      // pinkish
  image(img, 25, 0, 50, 90);

  img.loadPixels();

  for (var x = 0; x &lt; img.width; x++) {
      for (var y = 0; y &lt; img.height; y++) {
             var index = (x + y * width)*4;
         var pixC = img.get(index);
    //     console.log(pixC);
         if (pixC !=  color('255, 0, 0, 50'))    // background color   // I also tried "if (pixC ==  color('0, 0, 0'))" 
             {  
            console.log("OK");      
            pixels[index+0] = 0;
            pixels[index+1] = 255;
            pixels[index+2] = 0;
            pixels[index+3] = 255;           
         }
      }
  }

  img.updatePixels();
  image(img, 25, 0, 50, 90);
}
</code></pre>

<p>Thanks for your help.</p>
]]></description>
   </item>
   <item>
      <title>Trouble Assigning ASCII Values to Jump Points in Audio File</title>
      <link>https://forum.processing.org/two/discussion/27870/trouble-assigning-ascii-values-to-jump-points-in-audio-file</link>
      <pubDate>Sat, 28 Apr 2018 18:17:30 +0000</pubDate>
      <dc:creator>sammy</dc:creator>
      <guid isPermaLink="false">27870@/two/discussions</guid>
      <description><![CDATA[<p>Hi! I am trying to make a simple audio sampler by triggering 1 second segments of a 10 second audio file from the computer keyboard. In the code below, I'm trying to trigger second 1 of the file with the 'a' key, second 2 with the 's' key, and so on.</p>

<p>At the moment, this code doesn't make any sound, though I got the keyTyped() <a rel="nofollow" href="https://p5js.org/reference/#/p5/keyTyped">example</a> to trigger changes in a sketch. Is the problem in my use of <code>song.playMode('restart')</code>? Or should I <code>preload()</code> the sound? Or do I need to assign number ASCII values to the samples?</p>

<p>Thanks for any help!</p>

<pre><code>var song;

function setup(){
  song = loadSound('10sec.mp3');
}

function keyTyped() {
    song.playMode('restart');
    if (keyCode === 'a') {
        song.jump(0, 1);
    } else if (keyCode === 's') {
        song.jump(5, 1);
    } else if (keyCode === 'd') {
        song.jump(2, 1);
    }
}
</code></pre>
]]></description>
   </item>
   <item>
      <title>Chrome extensions with gif in canvas</title>
      <link>https://forum.processing.org/two/discussion/25534/chrome-extensions-with-gif-in-canvas</link>
      <pubDate>Tue, 12 Dec 2017 13:49:01 +0000</pubDate>
      <dc:creator>Layzfat</dc:creator>
      <guid isPermaLink="false">25534@/two/discussions</guid>
      <description><![CDATA[<p>Hi,</p>

<p>I'm follow Shiffman incredible lessons on using P5js with chrome extensions.</p>

<p>In the example of the Canvas sketch with chrome extensions I followed, I wanted to do some more. Add gifs in the canvas overlaying the top layer of the browser viewing.</p>

<p>My attempt was unsuccessful.. The gif doesn't play..</p>

<p>So what did I do. 
I used <a rel="nofollow" href="https://p5js.org/examples/image-load-and-display-image.html">this example</a> to import gif in to p5js 
put it in my sketch.js as this:</p>

<pre><code>console.log('sketch blah');

var s = function(sketch) {
var img; //declare image

sketch.setup = function() {
  document.body.style['userSelect'] = 'none';
  let h = document.body.clientHeight;
  let c = sketch.createCanvas(sketch.windowWidth, h);
  img = sketch.loadImage("https://" + "media.giphy.com/media/xUOxfnlpG1tsYGFI40/giphy.gif");
  c.position(0,0);
  c.style('pointer-events', 'none');
  sketch.clear();
}

sketch.draw = function() {
  console.log('sketch Looping');
  sketch.image(img, 0, 0);
  //sketch.background(0);
  }
};

var myp5 = new p5(s);
</code></pre>

<p>My manifest.json looks like this.</p>

<pre><code>  {
    "manifest_version": 2,
    "name": "Agent Extension",
    "version": "0.0002",
    "web_accessible_resources": [
      "gif/*.gif"
    ],
    "content_scripts": [
      {
        "matches": [
            "&lt;all_urls&gt;"
        ],
        "js": ["p5.js","p5.dom.js","p5.gif.js","p5.gif.min.js", "sketch.js"]

      }
    ],
    "background": {
      "scripts": ["background.js"]
    },
    "browser_action": {
      "default_icon": "img.png"
    }
  }
</code></pre>

<p>all the files are in the wright place.</p>

<p>I get this error in the browsers console</p>

<pre><code>sketch.js:18 Uncaught ReferenceError: image is not defined
    at sketch.draw (sketch.js:18)
    at p5.redraw (p5.js:50266)
    at p5.&lt;anonymous&gt; (p5.js:44917)
    at p5.&lt;anonymous&gt; (p5.js:44812)
sketch.draw @ sketch.js:18
p5.redraw @ p5.js:50266
(anonymous) @ p5.js:44917
(anonymous) @ p5.js:44812
</code></pre>

<p>I did define it, my guess it is not defined for the browsers standards</p>

<p>I want to have a few 'objects' on the canvas and other types of code taking over the content as end goal.</p>
]]></description>
   </item>
   <item>
      <title>How to upload image in p5.js mode</title>
      <link>https://forum.processing.org/two/discussion/25418/how-to-upload-image-in-p5-js-mode</link>
      <pubDate>Wed, 06 Dec 2017 18:55:57 +0000</pubDate>
      <dc:creator>asdf</dc:creator>
      <guid isPermaLink="false">25418@/two/discussions</guid>
      <description><![CDATA[<p>I would like to upload an image file named "dillustration-01.png" (dimension: 1995 × 1667pixels) in p5.js mode. But if I use the following code, I get a blank screen with no image. I double checked that I have added the file "dillustration-01.png" in my sketch and I don't know why it's not working. Is there anything wrong with what I'm doing?</p>

<pre><code>var img;  // Declare variable 'img'.

function setup() {
  createCanvas(1000, 1000);
  img = loadImage("dillustration-01.png");  // Load the image
}

function draw() {
  // Displays the image at its actual size at point (0,0)
  image(img, 0, 0);
  // Displays the image at point (0, height/2) at half size
  //image(img, 0, height/2, img.width/2, img.height/2);
}
</code></pre>
]]></description>
   </item>
   <item>
      <title>Displaying animation before the sketch starts.</title>
      <link>https://forum.processing.org/two/discussion/25405/displaying-animation-before-the-sketch-starts</link>
      <pubDate>Wed, 06 Dec 2017 01:19:52 +0000</pubDate>
      <dc:creator>UsamaRehan</dc:creator>
      <guid isPermaLink="false">25405@/two/discussions</guid>
      <description><![CDATA[<p>I have a sketch where there are multiple sounds &amp;&amp; images are loaded in the <code>preload()</code> function. It takes about 5 seconds to load everything and then the sketch starts.</p>

<p>For this 5 seconds,the screen goes black. Instead of that I want to display some little animation which would keep the user active instead of looking at the black empty screen.</p>

<p>How can I do this?</p>
]]></description>
   </item>
   <item>
      <title>loadStrings working weirdly</title>
      <link>https://forum.processing.org/two/discussion/24549/loadstrings-working-weirdly</link>
      <pubDate>Sat, 14 Oct 2017 17:51:13 +0000</pubDate>
      <dc:creator>TheZipCreator</dc:creator>
      <guid isPermaLink="false">24549@/two/discussions</guid>
      <description><![CDATA[<p>So I have this bit of code in preload()</p>

<pre><code>function preload() {
  let gamename = "test";
  let fdat = loadStrings("games/"+gamename+".bit");
  console.log(fdat);
  fdata = fdat;
}
</code></pre>

<p>And the file "test.bit" reads:</p>

<pre><code>block
50
110
200
200
0
255
0
floor
50
100
200
10
0
255
0
</code></pre>

<p>But what I get in console is an array containing the values:</p>

<pre><code>"block"
"50"
"110"
"200"
"200"
"0"
"255"
"0"
</code></pre>

<p>Even though the file reads what is above.</p>

<p>The array should read:</p>

<pre><code>"block"
"50"
"110"
"200"
"200"
"0"
"255"
"0"
"floor"
"50"
"100"
"200"
"10"
"0"
"255"
"0"
</code></pre>

<p>I have no idea why this happens, can someone explain?</p>
]]></description>
   </item>
   <item>
      <title>P5js How to load an image and subsequently alter it?</title>
      <link>https://forum.processing.org/two/discussion/24359/p5js-how-to-load-an-image-and-subsequently-alter-it</link>
      <pubDate>Tue, 03 Oct 2017 17:11:48 +0000</pubDate>
      <dc:creator>codedanddied</dc:creator>
      <guid isPermaLink="false">24359@/two/discussions</guid>
      <description><![CDATA[<p>I am having issues loading an image from my sprites folder, which is in the same directory as my index.html.</p>

<pre><code>    function draw() {
        background(255);
        image(loadImage("sprites/running0.png"), 0, 0);
    }
</code></pre>

<p>I'm receiving this error:</p>

<blockquote class="Quote">
  <p>p5.js says: It looks like there was a problem loading your image. Try checking if the file path [file:///C:/Users/Pradeep/Documents/.../.../src/sprites/running0.png] is correct, hosting the image online, or running a local server.[<a href="https://github.com/processing/p5.js/wiki/Local-server]" target="_blank" rel="nofollow">https://github.com/processing/p5.js/wiki/Local-server]</a> &gt;</p>
</blockquote>

<p>The source is correct so I have no idea what to do next since I think I'm correctly pointing it the file? Do I really have to run a private local server to circumnavigate security blocks? What are these security blocks that disable me from loading images if any?</p>

<p>Finally, once an image is loaded, is it possible to alter its pixel values, such as draw vertical and horizontal white lines across its entire image-width/image-length using simple for loops? I'm trying to do the same with a Python script (dependencies = matplotlib, scipy, numpy) but matplotlib saving figures is extremely awkward if you're just "editing images" rather than plotting mathematical stuff. If anyone knows any form of coding image pixel manipulation that is simpler please let me know! No I will not draw over my image using Paint. ;)</p>

<p>Ultimately I would like to load images such that my sprite (8 images) seems to run over draw:</p>

<pre><code>    var i = 0;
    function draw() {
        background(255);
        image(loadImage("sprites/running" + i + ".png"), 0, 0);
        i++;
        if (i == 8) {
            i = 0;
        }
    }
</code></pre>
]]></description>
   </item>
   <item>
      <title>preloading many sounds (in array)?</title>
      <link>https://forum.processing.org/two/discussion/24120/preloading-many-sounds-in-array</link>
      <pubDate>Thu, 14 Sep 2017 14:17:29 +0000</pubDate>
      <dc:creator>xna</dc:creator>
      <guid isPermaLink="false">24120@/two/discussions</guid>
      <description><![CDATA[<p>What's the correct way to preload a lot of samples? Instead of just one file like shown in this video: <a rel="nofollow" href="https://youtu.be/Pn1g1wjxl_0?t=4m57s">11.1: Loading and Playing - p5.js Sound Tutorial </a></p>
]]></description>
   </item>
   <item>
      <title>Transforming PVectors into p5.vector (or createVector?)</title>
      <link>https://forum.processing.org/two/discussion/24074/transforming-pvectors-into-p5-vector-or-createvector</link>
      <pubDate>Sat, 09 Sep 2017 06:17:16 +0000</pubDate>
      <dc:creator>paul_young1984</dc:creator>
      <guid isPermaLink="false">24074@/two/discussions</guid>
      <description><![CDATA[<p>Hi Folks,</p>

<p>I've been reading through every 'convert Processing into P5' document I can get a hold of, and I still can't figure out how to translate PVector's (processing) into p5.vector syntax.</p>

<p>Beneath is code in Processing (working). Beneath that is my code for p5 (not working) I'm pretty sure it's my Vector syntax that is out of wack. Can someone please provide direction? Saying that, there might be other things wrong, and I'm open to suggestions.</p>

<p>PROCESSING:</p>

<pre><code>PImage img;
ArrayList pts;


void setup() {
  img = loadImage("ASKEW A.jpg");

    size(500, 500, P3D);

  pts = new ArrayList();
  findPoints();
}

void draw () {
  background(255);


for (int i = pts.size()-1; i &gt;= 0; i --){
      PVector pt = (PVector)pts.get(i);

      fill(pt.z);

      noStroke();

      stroke(0, 30);
      for (int j = pts.size()-1; j &gt;= 0; j --){
      if(i&gt;1){  
        PVector pt2 = (PVector)pts.get(j);
        if (pt.dist(pt2) &lt; 20){

          int r = 10;
          float x = r * tan(radians(i*(360.0/ (mouseX * tan(3)) )));
          float y = r * sin(radians(i*(360.0/ (mouseY*  tan(3)) )));

          int r1 = 2;
          float x1 = r1 * tan(radians(i*(360.0 / (mouseX* tan(3)))));
          float y1 = r1 * sin(radians(i*(360.0 / (mouseY* tan(3)))));


          stroke(0, 30);
          line(pt.x + x, pt.y + y, 
          pt2.x + x1, pt2.y + y1);
        }
      }
    }
  }
}

void findPoints(){

  img.loadPixels();
  for (int i = img.width-1; i &gt;= 0; i -= 3){
    for (int j = img.height; j &gt;=0; j -= 3){

      color c = img.get(i, j);
      if (brightness(c) &lt; 255){
        pts.add(new PVector(i, j, brightness(c)));
      }
    }
  }
}
</code></pre>

<p>p5 :</p>

<pre><code>var img;
var pts = [];
var i, j;


function setup() {

  createCanvas(500, 500);

    img = loadImage("ASKEW A.jpg");
    findPoints();
    }


function draw () {
  background(255);

for (i = pts.length()-1; i &gt;= 0; i --){
      var pt = new p5.Vector.pts.get(i);
      fill(pt.z);

      noStroke();

      stroke(0, 30);
      for (j = pts.length()-1; j &gt;= 0; j --){
      if(i&gt;1){  
        var pt2 = new p5.Vector.pts.get(j);
        if (pt.dist(pt2) &lt; 20){

          var r = 20;
          var x = r * tan(radians(i*(360.0/ (mouseX) )));
          var y = r * sin(radians(i*(360.0/ (mouseY) )));

          var r1 = 2;
          var x1 = r1 * tan(radians(i*(360.0 / (mouseX))));
          var y1 = r1 * sin(radians(i*(360.0 / (mouseY))));


          stroke(0, 30);
          line(pt.x + x, pt.y, 
          pt2.x + x1, pt2.y + y1);
        }
      }
    }
  }
}


function findPoints(){

  img.loadPixels();
  for (var i = img.width-1; i &gt;= 0; i -= 3){
    for (var j = img.height; j &gt;=0; j -= 3){

      var c = img.get(i, j);
      if (brightness(c) &lt; 255){
        pts.push(new p5.Vector(i, j, brightness(c)));
      }
    }
  }
}
</code></pre>
]]></description>
   </item>
   <item>
      <title>Google Chrome Image() function problem</title>
      <link>https://forum.processing.org/two/discussion/23465/google-chrome-image-function-problem</link>
      <pubDate>Sun, 16 Jul 2017 14:15:25 +0000</pubDate>
      <dc:creator>DominikN</dc:creator>
      <guid isPermaLink="false">23465@/two/discussions</guid>
      <description><![CDATA[<p>I made an animation in p5 Javascript. This animaiton has a backgroundimage. This Image is displayed perfectly in firefox and all other browsers except Google Chrome.</p>

<p>Can somebody help me?</p>

<h2>My JS code:</h2>

<pre><code>var canvas;
var backgroundimage;

var type1_Rect = [];
var type1_Rect_amount = screen.width * 0.2;
var type1_Rect_speed = 1;
var type1_Rect_size = 1;

//-----------------------
var type2_Rect = [];
var type2_Rect_amount = screen.width * 0.1;
var type2_Rect_speed = 0.7;
var type2_Rect_size = 2;
var j = 0;
//------------------------

var i = 0;
function windowResized() {
    resizeCanvas(window.innerWidth, window.innerHeight);
}
function setup() {
    backgroundimage = loadImage("wallpaper/wallpaper.png"); //The Backgroundimage

    canvas = createCanvas(window.innerWidth, window.innerHeight);
    canvas.position(0, 0);
    canvas.style('z-index', '-10');

    for (i = 0; i &lt; type1_Rect_amount; i++) {
    type1_Rect[i] = {
        x: random(0, screen.width),
        y: random(0, screen.height),

        display: function() {
            fill(255);
            noStroke();
            rect(this.x, this.y, type1_Rect_size, type1_Rect_size);
        },

        move: function() {
            this.y = this.y - type1_Rect_speed;

            if(type1_Rect[i].y &lt;= 0) {
                type1_Rect[i].y = window.innerHeight;
            }
        }
    }
}
i = 0;

//-------------------------------------
for (j = 0; j &lt; type2_Rect_amount; j++) {
type2_Rect[j] = {
    x: random(0, screen.width),
    y: random(0, screen.height),

    display: function() {
        fill(255);
        noStroke();
        rect(this.x, this.y, type2_Rect_size, type2_Rect_size);
    },

    move: function() {
        this.y = this.y - type2_Rect_speed;

        if(type2_Rect[j].y &lt;= 0) {
            type2_Rect[j].y = window.innerHeight;
        }
    }
}
}
j = 0;
 //---------------------------------------
}

function draw() {

image(backgroundimage, 0, 0);      //Backgroundimage

fill(255);
      //text(screen.width, 50, 50);
      //text(type1_Rect_amount, 50, 70);
      //text(type2_Rect_amount, 50, 90);

while (i &lt; type1_Rect_amount) {
type1_Rect[i].display();
type1_Rect[i].move();

i++;
}
i = 0;

      //----------------------------
while (j &lt; type2_Rect_amount) {
type2_Rect[j].display();
type2_Rect[j].move();

j++;
}
j = 0;
      //----------------------------
}
</code></pre>
]]></description>
   </item>
   <item>
      <title>trying to set the font, getting a black screen instead</title>
      <link>https://forum.processing.org/two/discussion/23410/trying-to-set-the-font-getting-a-black-screen-instead</link>
      <pubDate>Wed, 12 Jul 2017 03:54:56 +0000</pubDate>
      <dc:creator>mortebouse</dc:creator>
      <guid isPermaLink="false">23410@/two/discussions</guid>
      <description><![CDATA[<p>hello, i'm trying to code a short interactive animation in openprocessing with images, sound, and text, and when i try to use textFont() in preload(), my whole sketch goes black (though the interactivity and sound both remain). and if i try to use it in draw() or setup(), it sets itself to a different font instead and sets the font size to the same size across the sketch.</p>

<p>the only time it has worked when i tried (but not with the typeface i need) it is when i set it to textFont('Courier') in draw(). i've tried to load another .ttf file to see if the font itself was the problem, but the replacement one didn't work either. the font i'm trying to load is <a rel="nofollow" href="https://fontstruct.com/fontstructions/show/675134/fontstuck_2">this one</a>.</p>

<p>you can find my sketch <a rel="nofollow" href="https://www.openprocessing.org/sketch/438767">here</a>. it takes a little bit of time to load. i would post the code here, too but there are a dozen files needed to play it locally, so i think it's better if i don't. if you need more information, let me know.</p>

<p>thanks in advance.</p>
]]></description>
   </item>
   <item>
      <title>Read JSON and images in preload()</title>
      <link>https://forum.processing.org/two/discussion/23174/read-json-and-images-in-preload</link>
      <pubDate>Fri, 23 Jun 2017 03:04:16 +0000</pubDate>
      <dc:creator>reona396</dc:creator>
      <guid isPermaLink="false">23174@/two/discussions</guid>
      <description><![CDATA[<p>Hello, I have a question about p5.js preload function.</p>

<p>I want to read JSON and images in my p5.js sketch,<br />
and JSON has images information (number of files, names, id).<br />
So I write a code and JSON as follows, but it doesn't work properly.<br />
I think JSON value  is not read normally or not on time in preload function.<br />
I wonder how I can resolve this problem. Please your help!</p>

<hr />

<pre><code>var data;  
var images = [];  
var itemNum;  

function preload() {  
    data = loadJSON("./data.json");  

    itemNum = Object.keys(data).length;  
    console.log(itemNum); // output "0", but this is actually "3"  

    for (var i = 0; i &lt; itemNum; i++) {  
        images[i] = loadImage("./images/image" + data[i].imageId + ".png");  
    }  
}  

function setup() {  
    createCanvas(100, 100);  
    image(images[0], 0, 0);  
}  
</code></pre>

<hr />

<pre><code>[{
        "name": "AAA",
        "imageId": "100"
    },
    {
        "name": "BBB",
        "imageId": "200"
    },
    {
        "name": "CCC",
        "imageId": "300"
    }
]
</code></pre>
]]></description>
   </item>
   <item>
      <title>On p5JS canvas changing background image showing already drawn objects behind the background image</title>
      <link>https://forum.processing.org/two/discussion/22026/on-p5js-canvas-changing-background-image-showing-already-drawn-objects-behind-the-background-image</link>
      <pubDate>Sun, 16 Apr 2017 07:21:26 +0000</pubDate>
      <dc:creator>Adeeba</dc:creator>
      <guid isPermaLink="false">22026@/two/discussions</guid>
      <description><![CDATA[<p>Hi,</p>

<p>I'm drawing some objects on canvas but if later I change the background image on demand, then already drawn objects are behind the background image. How to bring already drawn objects in front. Below is the sample code i'm using to change the background image.</p>

<p>function draw() {
    if(chnBg){  //if change background is clicked
        loadImage("images/Grid_Image.png",function(bg){<br />
                background(bg);<br />
        });
    }
}</p>
]]></description>
   </item>
   <item>
      <title>P5.js svg image array loading problem | please help</title>
      <link>https://forum.processing.org/two/discussion/21747/p5-js-svg-image-array-loading-problem-please-help</link>
      <pubDate>Sat, 01 Apr 2017 16:46:03 +0000</pubDate>
      <dc:creator>anetecima</dc:creator>
      <guid isPermaLink="false">21747@/two/discussions</guid>
      <description><![CDATA[<p>HI!
I started exploring P5.js really recently and i haven't coded javascript for a while, so i really need help.
i think it's probably nothing big, but i can't get around this problem.</p>

<p>i have an array of objects that contain svg images from preloaded image array.
objects have two methods - update and fsize
update displays the array, but size changes the sizse of each image each time displayed.</p>

<p>when i run the code, usually it doesn't fill the whole screen and gives me this:</p>

<p><em>Uncaught TypeError: Failed to execute 'drawImage' on 'CanvasRenderingContext2D': The provided value is not of type '(CSSImageValue or HTMLImageElement or HTMLVideoElement or HTMLCanvasElement or ImageBitmap or OffscreenCanvas)'
    at p5.Renderer2D.image (p5.js:14621)
    at p5.image (p5.js:20773)
    at Letter.update (sketch.js:71)
    at draw (sketch.js:46)
    at p5.redraw (p5.js:16560)
    at p5.resizeCanvas (p5.js:15974)
    at windowResized (sketch.js:102)
    at p5._onresize (p5.js:12789)</em></p>

<p>but there are times when it does fill the whole screen and runs without any problem.
Can someone please help me with this?</p>

<p>the code:</p>

<pre><code>/*
Created by anetecima
27/03/2017
*/



        var characters = [];
        var Angle;
        var space;
        var letters = [];

        function preload() {
          characters = [66];
          for(var i = 1; i&lt;67; i++){
            characters[i] = new Image();
            characters[i] = loadImage("burti/"+i+".svg");               
          }
        }

        function setup(){
        createCanvas(windowWidth,windowHeight);
        space = 120;
        Angle = 0;
        imageMode(CENTER);
        background(255);
        noStroke();

        var wide = width / space;
        var high = height / space;

        //AIZPILDA LAUKUMU AR BURTIEM
        var tf = [ 1,0 ];
        for (var i = 0; i &lt; wide; i++) {
          for (var j = 0; j &lt; high; j++) {
            letters.push(new Letter(i,j,random(characters),random(tf),random(15,60)));
          }
        }
        }

        function draw(){
        background(255);
        translate(50,50);
        Angle = frameCount*0.06;

        for (var i = 0; i&lt;letters.length; i++) {
          push();
            letters[i].update();
            letters[i].fsize();
          pop();
          }
        }

        function Letter( ixC, iyC,  ia, idirection,  isize, degreesss) {
        this.xC = ixC;
        this.yC = iyC;
        this.a = ia;
        this.direction = idirection;
        this.size = isize;


        this.update = function(){
            translate(this.xC*space, this.yC*space);
            push(); 
                rotate((Angle+(this.xC+this.yC*100)));
                image(this.a, 0,0,this.a.width * (this.size*0.01),this.a.height * (this.size*0.01));
            pop();

          };


          this.fsize = function(){

          if(this.direction==0){
                this.size--;
            if(this.size&lt;=15){
              this.direction=1;
            }
          }
          if(this.direction==1){
          this.size++;  
            if(this.size&gt;=60){
              this.direction=0;
            }  
          }
          };

        }

        function mouseClicked() {
          for (var i = 0; i &lt; letters.length; i++) {
              letters[i].a = random(characters);
              letters[i].size = random(15,60);
          }
        }
        function windowResized() {
          resizeCanvas(windowWidth, windowHeight);
        }
</code></pre>

<p>`</p>
]]></description>
   </item>
   <item>
      <title>Why does P5.js say "Loading..."?</title>
      <link>https://forum.processing.org/two/discussion/20194/why-does-p5-js-say-loading</link>
      <pubDate>Wed, 11 Jan 2017 15:49:05 +0000</pubDate>
      <dc:creator>lola</dc:creator>
      <guid isPermaLink="false">20194@/two/discussions</guid>
      <description><![CDATA[<p>Why does P5.js say "loading..." when I play my sketch through either P5 or the index.html?</p>
]]></description>
   </item>
   <item>
      <title>loadJSON() displayes "loading..."</title>
      <link>https://forum.processing.org/two/discussion/19728/loadjson-displayes-loading</link>
      <pubDate>Tue, 13 Dec 2016 18:39:53 +0000</pubDate>
      <dc:creator>janwotto</dc:creator>
      <guid isPermaLink="false">19728@/two/discussions</guid>
      <description><![CDATA[<p>hi made a sprite editor that saves the sprite in a JSON format. When i try to load the JSON with loadJSON() in the pre load function the script will not run.  Only the text " loading..." is displayed.  I don't think I am doing something wrong here because even the reference page in on the p5 site shows the same bug. i tried different browsers and it has the same i issue. do i need to report this some where or is this a know issue?  is there maybe a alternative for loadJSON() ?</p>

<p>here is a print screen from this issue on the reference page where the left canvas wouldn't load.</p>

<p><img src="https://forum.processing.org/two/uploads/imageupload/680/OZ7GQH4P7J2F.png" alt="Screenshot 2016-12-13 19.21.20" title="Screenshot 2016-12-13 19.21.20" /></p>
]]></description>
   </item>
   <item>
      <title>Load an SVG to a P5 sketch</title>
      <link>https://forum.processing.org/two/discussion/18851/load-an-svg-to-a-p5-sketch</link>
      <pubDate>Wed, 02 Nov 2016 20:33:13 +0000</pubDate>
      <dc:creator>SAFR</dc:creator>
      <guid isPermaLink="false">18851@/two/discussions</guid>
      <description><![CDATA[<p>I've been programming in Processing some time now. I've also worked with shapes and SVG files. Having this humble experience regarding SVG files in Processing made me think that it would be the same story in P5.js, which is clearly not the case and makes me seek for help.</p>

<p>Back in Processing I would just have simple code like this:</p>

<pre><code>PShape shape;
/***************************************************************************/
void setup() 
{
  size(400, 400);
  shapeMode(CENTER);
  shape = loadShape("bot1.svg");
} 
/***************************************************************************/
void draw() 
{
  background(100);
  pushMatrix();
  translate(width/2, height/2);
  shape(shape, 0, 0);
  popMatrix();
}
</code></pre>

<p>P5 doesn't work that way. What would be the P5.js equivalent to that?</p>

<pre><code>    var shape;
    var canvas;
/***************************************************************************/
    function setup() 
    {
      canvas = createCanvas(400, 400);
      canvas.position(0, 0);
      //shapeMode(CENTER);
      //shape = loadShape("bot1.svg");
    } 
    /***************************************************************************/
    void draw() 
    {
      background(100);
      push();
      translate(width/2, height/2);
      //shape(shape, 0, 0);
      pop();
    }
</code></pre>
]]></description>
   </item>
   <item>
      <title>Loading new data - preload again?</title>
      <link>https://forum.processing.org/two/discussion/18595/loading-new-data-preload-again</link>
      <pubDate>Mon, 17 Oct 2016 14:04:20 +0000</pubDate>
      <dc:creator>AndyD</dc:creator>
      <guid isPermaLink="false">18595@/two/discussions</guid>
      <description><![CDATA[<p>In order to load data with loadStrings(), I assume I need to use preload() - at least, that's what my first test shows :)</p>

<p>But if I want to be able to load another text file while a sketch is running (to load the next level in a game, for instance) can I call preload again? I assume that could cause trouble since it would "preload" other things that are already loaded.</p>

<p>I haven't used callbacks, but is that an alternative to preloading when using loadStrings()? If so, what would it look like.</p>

<p>Also, since the data (a list of numbers) loads as strings, is there an easy way to convert them to numbers as they load into the array? Here's the loading line of code"</p>

<p><code>gridSquare = loadStrings('data.txt');</code></p>

<p>But after much banging of my head wondering why the data wasn't doing what it should, , I realised it does exactly what it says and loads strings. I assume I need split. again, but how?</p>
]]></description>
   </item>
   <item>
      <title>Conditional background() of an image in draw() fails on OS X.</title>
      <link>https://forum.processing.org/two/discussion/18312/conditional-background-of-an-image-in-draw-fails-on-os-x</link>
      <pubDate>Tue, 27 Sep 2016 16:59:50 +0000</pubDate>
      <dc:creator>jet</dc:creator>
      <guid isPermaLink="false">18312@/two/discussions</guid>
      <description><![CDATA[<p>Running in to a strange problem with p5 0.6.1 on the Mac -- I can get a background image to load sometimes but not others.  The same script loads just fine on my Win8 VMWare partition, I'm using Firefox in OS X and Win8.</p>

<pre><code>var bg;
var backgroundLoaded = false;

function setup() {
  bg = loadImage("assets/moonwalk.png");
  createCanvas(720, 400);
}

function draw() {
  // this works, loads the background image on every cycle and overrwites my ellipses:
  // background(bg);

  if (backgroundLoaded == false) {
    // this works, turns the screen red, ellipses are not overwritten
    // background(255, 0, 0);
    // but this gives me a white screen:
    background(bg);
    backgroundLoaded = true;
  }

  ellipse(mouseX, mouseY, 10);
}
</code></pre>
]]></description>
   </item>
   <item>
      <title>passing a variable into a JSON URL in preload()</title>
      <link>https://forum.processing.org/two/discussion/18093/passing-a-variable-into-a-json-url-in-preload</link>
      <pubDate>Tue, 06 Sep 2016 19:55:55 +0000</pubDate>
      <dc:creator>tripleboss</dc:creator>
      <guid isPermaLink="false">18093@/two/discussions</guid>
      <description><![CDATA[<p>I'd like to pull weather information based on the user's location using the p5.geolocation library and Open Weather Map.
I'm able to access latitude and longitude outside of preload, but when I attempt to do so within preload, they both show "undefined".  What am I missing here?</p>

<pre><code>var locationData;

function preload() {
  locationData =  getCurrentPosition();

  var currentLat = locationData.latitude;
  var currentLon = locationData.longitude;

  var url = "//api.openweathermap.org/data/2.5/weather?lat=" + currentLat + "&amp;lon=" + currentLon + "&amp;appid=[myAPIkey]";

  weather = loadJSON(url);
}
</code></pre>
]]></description>
   </item>
   <item>
      <title>How to preload images using filenames from a .txt file</title>
      <link>https://forum.processing.org/two/discussion/18043/how-to-preload-images-using-filenames-from-a-txt-file</link>
      <pubDate>Fri, 02 Sep 2016 07:38:14 +0000</pubDate>
      <dc:creator>Hokk</dc:creator>
      <guid isPermaLink="false">18043@/two/discussions</guid>
      <description><![CDATA[<p>Hi I am currently working on a game project where I use a txt file that contains the sprite for an object, the object name, and some properties based on the object (ex: player.png, player, player's health). While the first two parts are not a problem to get working into the game, I cannot load the sprites due to only being able to run preload once which is spent on LoadStrings and so I cannot also load the images due to not being able to read the strings. Is there a way to force preload to wait until the LoadStrings function is done so that I can load the images that the txt file indicates?</p>

<p>Code Sample:</p>

<pre><code>    funtion preload(){
      mainFile = loadStrings("assets/MainFile.txt"); //load the text file
      //need it to wait here until loading strings finishes
      for (var i = 3; i &lt; mainFile.length; i++) {
        var newCharacterStr = splitTokens(mainFile[i], ",");
        var newImage = loadImage(newCharacterStr[0]);
      }
    }
</code></pre>

<p>MainFile.txt:</p>

<p>player.png,player,5</p>
]]></description>
   </item>
   <item>
      <title>Preload progress?</title>
      <link>https://forum.processing.org/two/discussion/17358/preload-progress</link>
      <pubDate>Wed, 29 Jun 2016 14:48:19 +0000</pubDate>
      <dc:creator>jfred1979</dc:creator>
      <guid isPermaLink="false">17358@/two/discussions</guid>
      <description><![CDATA[<p>Is there any way to get the overall progress of preload? I'd like to make a custom progress bar for sketch loading.</p>
]]></description>
   </item>
   <item>
      <title>loadFont() problem</title>
      <link>https://forum.processing.org/two/discussion/17113/loadfont-problem</link>
      <pubDate>Sun, 12 Jun 2016 18:24:13 +0000</pubDate>
      <dc:creator>Xeronimo74</dc:creator>
      <guid isPermaLink="false">17113@/two/discussions</guid>
      <description><![CDATA[<p>var myFont;</p>

<p>function preLoad() {
  myFont = loadFont('assets/DroidSansMono.ttf');
}</p>

<p>function setup() {
  textFont(myFont);
}</p>

<p>why does this give me the following error: "25722: Uncaught Error: null font passed to textFont"?</p>
]]></description>
   </item>
   <item>
      <title>Display Image</title>
      <link>https://forum.processing.org/two/discussion/16817/display-image</link>
      <pubDate>Wed, 25 May 2016 11:31:21 +0000</pubDate>
      <dc:creator>giulioisac</dc:creator>
      <guid isPermaLink="false">16817@/two/discussions</guid>
      <description><![CDATA[<p>It is a very basic question, I am trying to load an image that is inside the same folder of the index.html file but I can't. Here the code of the sketch:</p>

<pre><code>var img;
function setup() {
  createCanvas(windowWidth, windowHeight);
  img=loadImage("img1.png");
}

function draw() {
  image(img,0,0);
}
</code></pre>

<p>And here the code of the index.html:</p>

<pre><code>&lt;html&gt;
&lt;head&gt;
  &lt;title&gt;IMAGE&lt;/title&gt;

  &lt;script language="javascript" type="text/javascript" src="../../lib/p5.js"&gt;&lt;/script&gt;
  &lt;script language="javascript" type="text/javascript" src="sketch.js"&gt;&lt;/script&gt;

&lt;/head&gt;
&lt;body&gt;

&lt;/body&gt;
&lt;/html&gt;
</code></pre>

<p>What am I doing wrong? 
Thanks in advance!</p>
]]></description>
   </item>
   <item>
      <title>loading image from flickr query into draw( )</title>
      <link>https://forum.processing.org/two/discussion/16724/loading-image-from-flickr-query-into-draw</link>
      <pubDate>Fri, 20 May 2016 18:22:34 +0000</pubDate>
      <dc:creator>herringblue</dc:creator>
      <guid isPermaLink="false">16724@/two/discussions</guid>
      <description><![CDATA[<p>Why doesn't the image from the flickr query get displayed in draw( )?</p>

<pre><code>var api = "https://" + "api.flickr.com/services/rest/?method=flickr.photos.search";
var apiKey = "&amp;api_key=a0dd883dc062aadeaec4addb5211da7f";

var tag = "planet"

var query = "&amp;per_page=500&amp;tags=" + tag + "&amp;is_commons=true&amp;format=json&amp;nojsoncallback=1";

var i = 14;

var imgurl;
var img;

function preload() {
 var url = api + apiKey + query;

 loadJSON(url, gotData);


}

function setup() {
 createCanvas(300, 300);
 pixelDensity(1);


}

function gotData(data, imgurl) {

 var farmid = data.photos.photo[i].farm;
 var serverid = data.photos.photo[i].server;
 var id = data.photos.photo[i].id;
 var secret = data.photos.photo[i].secret;

 imgurl = "farm" + farmid + ".staticflickr.com/" + serverid + "/" + id + "_" + secret + ".jpg";

}

function draw() {
img = createImage(imgurl);
image(img,0,0);

}
</code></pre>
]]></description>
   </item>
   <item>
      <title>No errors and not working</title>
      <link>https://forum.processing.org/two/discussion/16544/no-errors-and-not-working</link>
      <pubDate>Wed, 11 May 2016 15:01:25 +0000</pubDate>
      <dc:creator>Epicman123456</dc:creator>
      <guid isPermaLink="false">16544@/two/discussions</guid>
      <description><![CDATA[<p>Hello, I am making my own version of space invaders for a school project. I am having issues getting this code to actually work. P5 isn't showing errors in the code, but it does show a suggestion to use [ ] instead of () (See code for the lines I'm referring to). When i switch to the square brackets, it shows an error. I'm not sure whats going on with that. Any help is appreciated. Thanks!!</p>

<pre><code>//initializes bullets
var bullets = {
    x: new Array(),
    y: new Array(),
    shot: new Array()
}
//initializes the ship
var ship = {
    x: 625,
    y: 475,
    photo: loadImage("download.png")
}

function setup() {
  createCanvas(1350,650);

  //bullet1
  append(bullets.x, ship.x);
  append(bullets.y, ship.y);
  append(bullets.shot, false);

  //bullet2
  append(bullets.x, ship.x);
  append(bullets.y, ship.y);
  append(bullets.shot, false);

  //bullet3
  append(bullets.x, ship.x);
  append(bullets.y, ship.y);
  append(bullets.shot, false);
}

//Controls
function updateShip()
{
    //Right movement
    if (keyIsDown(RIGHT_ARROW)) {
        ship.x = ship.x+ 10;
        if (ship.x &gt;= 1350) {
            ship.x = ship.x - 11;
        }
    }
    //Left movement
    if (keyIsDown(LEFT_ARROW)) {
        ship.x = ship.x - 10;
        if (ship.x &lt;= 0) {
            ship.x = ship.x + 11;
        }
    }
    //Up movement
    if (keyIsDown(UP_ARROW)) {
        ship.y = ship.y - 10;
        if (ship.y &lt;= 350) {
            ship.y = ship.y + 11;
        }
    }
    //Down movement
    if (keyIsDown(DOWN_ARROW)) {
        ship.y = ship.y + 10;
        if (ship.y &gt;= 580) {
            ship.y = ship.y - 11;
        }
    }
}

function drawShip()
{
    ship.photo.resize(75,75);
    image(ship.photo,ship.x-ship.photo.width/2,ship.y+ship.photo.height/2);
}

//Drawing the bullets
function drawBullets() {
    fill(255);
    rect(bullets.x[0],bullets.y[0], 5, 10);
    rect(bullets.x[1],bullets.y[1], 5, 10);
    rect(bullets.x[2],bullets.y[2], 5, 10);
}

//Controls the bullet movement
function updateBullets() {
    bullets.y[0] = bullets.y[0] + 10;
}

//Checks if bullet is shot
function checkShoot() {
    if (keyIsPressed &amp;&amp; keyCode === 32) {
        bullets.y[0] = ship.y;
    }
}

function draw() {
    background(0);

    updateShip();
    drawShip();
    checkShoot();
    updateBullets();
    drawBullets();
}
</code></pre>
]]></description>
   </item>
   <item>
      <title>Error on 'Video Pixels' example on P5JS Reference Page</title>
      <link>https://forum.processing.org/two/discussion/14044/error-on-video-pixels-example-on-p5js-reference-page</link>
      <pubDate>Thu, 17 Dec 2015 15:58:33 +0000</pubDate>
      <dc:creator>edhebert</dc:creator>
      <guid isPermaLink="false">14044@/two/discussions</guid>
      <description><![CDATA[<p>Hi-</p>

<p>I'm trying to better understand video manipulation, and think that an example in the P5JS Reference Guide can help me visualize what I need. However, this example is throwing an error to the console. i'm getting the JS error:</p>

<p><code>Failed to execute 'getImageData' on 'CanvasRenderingContext2D': The source width is 0.</code></p>

<p>Can someone help me understand why this code isn't working?</p>

<p>Thanks!</p>
]]></description>
   </item>
   <item>
      <title>preload()</title>
      <link>https://forum.processing.org/two/discussion/16468/preload</link>
      <pubDate>Fri, 06 May 2016 19:08:12 +0000</pubDate>
      <dc:creator>slow_izzm</dc:creator>
      <guid isPermaLink="false">16468@/two/discussions</guid>
      <description><![CDATA[<p>I have my sketch running just fine when I preview it out of p5 (other than my lack of splice() at the moment), but when I run my .html out of my root folder the browser window only loads my html while my .js sits on "Loading ... ". why is this?</p>

<pre><code>var izzms = [];
var imgs = [];
var j = 0;

function preload() {
  for (var i = 0; i &lt; 3; i++) {
    imgs[i] = loadImage('assets/proj' + i + '.jpg');
 }
}

function setup() {
  createCanvas(640, 480);
  image(imgs[j], 0, 0);
}

function draw() {
    for (var i = izzms.length - 1; i &gt;= 0; i--) {
    izzms[i].display();
 }
}

function mousePressed() {
  // var r = floor(random(imgs.length));
  var b = new Izzm(0, 0, imgs[j]);
  izzms.push(b);
  j++;
  if (j &gt; 2) {
    j = 0;
  }
}


//object
function Izzm(x, y) {
  this.x = x;
  this.y = y;

  this.display = function() {
    image(imgs[j], this.x, this.y);
 }
}
</code></pre>
]]></description>
   </item>
   <item>
      <title>Preloading lots of images?</title>
      <link>https://forum.processing.org/two/discussion/15389/preloading-lots-of-images</link>
      <pubDate>Wed, 09 Mar 2016 21:57:03 +0000</pubDate>
      <dc:creator>jm89</dc:creator>
      <guid isPermaLink="false">15389@/two/discussions</guid>
      <description><![CDATA[<p>Hey guys, I'm new to P5 but can't think of how to solve this issue. Basically, on page load, I want to preload a whole image folder so that lots of images can be used as css background-images without any loading occuring during the rest of the site visit.</p>

<p>Unfortunately, i'm working with over 100 images all of which are fairly high quality. When I switch between background-images, I get the slight flicker whilst it loads the next bg-image. Any advice on how to solve this issue?</p>
]]></description>
   </item>
   </channel>
</rss>