{"action":"closed","issue":{"url":"https://api.github.com/repos/processing/p5.js/issues/799","labels_url":"https://api.github.com/repos/processing/p5.js/issues/799/labels{/name}","comments_url":"https://api.github.com/repos/processing/p5.js/issues/799/comments","events_url":"https://api.github.com/repos/processing/p5.js/issues/799/events","html_url":"https://github.com/processing/p5.js/issues/799","id":94347746,"number":799,"title":"p5.Image.set() pixelDensity bug","user":{"login":"molleindustria","id":12369651,"avatar_url":"https://avatars.githubusercontent.com/u/12369651?v=3","gravatar_id":"","url":"https://api.github.com/users/molleindustria","html_url":"https://github.com/molleindustria","followers_url":"https://api.github.com/users/molleindustria/followers","following_url":"https://api.github.com/users/molleindustria/following{/other_user}","gists_url":"https://api.github.com/users/molleindustria/gists{/gist_id}","starred_url":"https://api.github.com/users/molleindustria/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/molleindustria/subscriptions","organizations_url":"https://api.github.com/users/molleindustria/orgs","repos_url":"https://api.github.com/users/molleindustria/repos","events_url":"https://api.github.com/users/molleindustria/events{/privacy}","received_events_url":"https://api.github.com/users/molleindustria/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":0,"created_at":"2015-07-10T17:03:13Z","updated_at":"2015-07-17T23:34:47Z","closed_at":"2015-07-17T23:34:47Z","body":"When you try to \"set\" an image instead of a single pixel inside another image p5 throws a \r\n\"TypeError: Cannot read property 'pixelDensity' of undefined\"\r\n\r\nThe line that causes the problem appears to be this one:\r\nhttps://github.com/processing/p5.js/blob/dbef89fdaa6c94648c534b4a0e869b63611e63c9/src/core/p5.Renderer2D.js#L274\r\n\r\nthis._pInst is undefined, or at least can be in certain cases. Try the example below to reproduce the problem.\r\n\r\nIn my particular case I solved it by adding  \r\n```var ctx = this._pInst || this;```\r\nand referring to ctx instead of this._pInst - mirroring what happens in the block immediately below the aforementioned line.\r\n\r\nBut I don't know enough about the renderer to push it as a fix. Hopefully somebody who worked on that module will check it out :)\r\n\r\n```\r\nfunction setup() {\r\n  createCanvas(100, 100);\r\n\r\n//create an image\r\nvar img = createImage(100, 100);\r\nimg.loadPixels();\r\nfor (i = 0; i < img.width; i++) {\r\n  for (j = 0; j < img.height; j++) {\r\n    img.set(i, j, color(0, 90, 102, i % img.width * 2));\r\n  }\r\n}\r\nimg.updatePixels();\r\n  \r\n//create a smaller image all red\r\nvar img2 = createImage(50, 50);\r\nimg2.loadPixels();\r\nfor (i = 0; i < img2.width; i++) {\r\n  for (j = 0; j < img2.height; j++) {\r\n    img2.set(i, j, color(255,0,0,100));\r\n  }\r\n}\r\nimg2.updatePixels();\r\n\r\n//set the pixels of the small one into the big one\r\nimg.loadPixels();\r\nimg.set(25,25,img2);\r\n//throws TypeError: Cannot read property 'pixelDensity' of undefined\r\nimg.updatePixels();\r\n  \r\nimage(img, 0, 0);\r\n}\r\n```"}}