How to access PGraphics pixels in p5.js - javascript

I'm trying to access pixels from a PGraphics instance in p5.js but even after calling loadPixels() the array is empty.
Here's what I've tried:
var buffer;
function setup() {
createCanvas(100,100);
pixelDensity(1);
buffer = createGraphics(100,100);
buffer.background(128);
}
function draw() {
image(buffer,0,0);
}
function mouseDragged(){
buffer.ellipse(mouseX,mouseY,3,3);
buffer.loadPixels();
console.log(buffer.pixels);//expecint pixels array, getting empty array
}
Is it possible to access pixels in PGraphics with p5.js ? If so, how ?

This appears to have been a bug: https://github.com/processing/p5.js/issues/1403
Other related bugs here and here.
It is fixed with the latest version (Version 0.5.3 from August 17, 2016).
If you're using the p5.js editor like me, then apparently that uses an old version of the library. You can download the latest version and copy it into your sketch folder. That seems to be persisting between runs of the editor, which is slightly scary, but maybe some magic is going on behind the scenes.
Anyway, your code works as expected after updating to the latest version.

Related

Openlayers 3 Replace a failed tile

We are running Open Layers 3.15.
Sometimes we get a dropped or failed tile.
Currently it displays nothing, (which can be confusing for our users) so we'd like to replace this with a tile that says 'no data' or something.
I've tried picking up the event and replacing the source of the tile eg
source.on('tileloaderror', function(){
source.setUrl('./images/map/failureTile.png');
});
but the problem with this is, instead of doing this on 1 tile, it does it for the entire layer, we don't want that.
Anyone know how we can do this for just the tile that failed and not the entire layer?
It's 2018, but for someone who may need this. Tested on v5.3.0
source.on('tileloaderror', function (event) {
var tileLoadFunction = function (imageTile, src) {
imageTile.getImage().src = './images/map/failureTile.png';
};
if (event.tile.tileLoadFunction_ != tileLoadFunction) {
event.tile.tileLoadFunction_ = tileLoadFunction;
event.tile.load();
}
});
This code relys on private function event.tile.tileLoadFunction_ is exposed.
Unfortunately xnakos' answer doesn't work on v5.3.0 because the event.tile.getImage() has been replaced with 1x1 canvas image by internal error handler.
Also noted, changing event.tile.src_ directly, seems to be an option, but it doesn't work either because of cache key or something.
A tile which failed to load should have a distinct class (.olImageLoadError). You can define a CSS rule not show these items.
.olImageLoadError {
display: none !important;
}
You could try this:
source.on('tileloaderror', function(event) {
event.tile.getImage().src = './images/map/failureTile.png';
});
You need the event parameter, that can get you the tile that failed, so that you can change the tile's image.
Warning: I tested the code above using tileloadend instead of tileloaderror, because my tiles never fail on me. :) I used a simulated failure rate using Math.random() and some random tiles were replaced by the image specified. I cannot think of a reason the code above would not work. If you verify it works, I will remove this warning from my answer. I tested it on OpenLayers 3.14.2 and on an OSM source.

IE 11 Out Of StackSpace

I have a rather large web app mostly written and tested on chromium. While testing it on IE11 it works for a while but then throws Out of stack space, and Stack Overflow at line 0. It is offering literally no way of debugging the issue. Does Internet Explorer handle circular structures differently by any chance? or does it run a limited memory per webpage. My computers task manager shows that IE is using around 1% of my memory where I have 12GB installed.
I have many Objects in this app such as the following...Not an actual snippet just an example.
var Classemployee=function(e){
this.id=e[0];
this.orders=[];
this.prefrences={
somePref:true,
1:blah,
........
}
}
var classOrder= function(e){
this.id=e[0];
this.server=employees[e[1]];
this.status=0;
this.seats={
1: new classSeat(this, 1),
..........
};
}
var classSeat = function(parent, id){
this.id=id;
this.parent=parent;
this.children=[
new classOrderLine(this.id, this.parent, arrayOfLineItemInformation)
]
}
var classOrderLine(parent, order, e){
this.id=e[0];
this.parent=parent;
this.order=order;
}
var orders={}
currentOrder=new classOrder(e);
orders[e[0]]=currentOrder;
The only thing i can think of is IE isn't handling these circular references properly but can't find any information on this subject.
After reviewing code further I found the problem was with a piece of code that was monitoring changes throughout the softwares memory By creating a hash of all of serialized objects. The way this code was written didn't realize there was a circular reference and continued looping through and for example storing data of:
orderline.parent.orderline.parent.etc....

An object disappears after packing a JavaScript library

I have created a JavaScript library and packed it with these options selected : Shrink Variables and Base62 Encoded at this url: http://dean.edwards.name/packer/. In this library I have declared an object ax, but when I use the packed version in my web page I get an error saying Uncaught ReferenceError: ax is not defined.
The original code of this library looks like below.
var ax = {
scaleUp:function(win) {
//code omitted
},
downGrade:function(win) {
//code omitted
}
}
In my web page in which I am using this library, I have code like below. This code works, if instead of packing, I minify it using Microsoft's Minifier or just use the original JavaScript library without minification or packing.
var result = ax.downGrade(w);
Question :
Why is the variable ax not accessible with packed version? Do I need to add something else when using the packed version?
UPDATE 1:
I could not get the packed file to work but packing my code through another compression utility at following url worked in my case: http://jsutility.pjoneil.net/. It provided an equally good compression.
I am still not sure why the utility at original url failed to produce a working version of my library, even though my original code works without any errors on any web page.
Check your console for errors before trying to call ax. Explicitly place semi-colons where they belong.Example at the end of the definition for ax you should put a semi-colon, even though in standard code it's good as is. Remove the explicit var declarations. When I did these things:
ax = {
scaleUp:function(win) {
alert("up");
},
downGrade:function(win) {
alert("down");
}
};
result = ax.downGrade();
Ran without issue in jsFiddle and console: http://jsfiddle.net/7kdnw65n/. I suspect it has to do with how the algorithm "shrinks" the variables. The resulting pack was:
eval(function(p,a,c,k,e,r){e=String;if(!''.replace(/^/,String)){while(c--)r[c]=k[c]||c;k=[function(e){return r[e]}];e=function(){return'\\w+'};c=1};while(c--)if(k[c])p=p.replace(new RegExp('\\b'+e(c)+'\\b','g'),k[c]);return p}('0={5:1(2){3("6")},4:1(2){3("7")}};8=0.4();',9,9,'ax|function|a|alert|downGrade|scaleUp|up|down|result'.split('|'),0,{}))

Finding the variable of a Canvas Element in unobfuscated Javascript

I am working with a GameMaker Studio and exporting a game in HTML5.
A missing feature in GameMaker Studio is the ability to export an image of the screen to the photo album of iOS and Android devices.
My workaround idea is to use a 3rd party plugin for HTML5 that will allow me to save a Canvas.
However GameMaker obfuscates the final output of the javascript and canvas it creates.
All I need to do is find which global variable contains the Canvas and I can capture it.
I have gone through a sample of unobfuscated Javascript but I can't read the code well enough to know which global variable I am supposed to use to call the canvas in question (and I don't want to override the context of it will wipe the canvas).
I found where the Canvas is attached to the context. Can anyone help here?
A sample looks like this:
_j61.prototype._q61 = function () {
_C8 = document.getElementById("canvas").getContext("2d");
if (_C8) {
this._yr = new _e9._B9._ua();
this._yr._Gp(_C8);
this._yr._Jp(1.0 / this._xW);
this._yr._Rp(0.1);
this._yr._Mp(1.0);
this._yr._Bp(_e9._B9._ua._2q | _e9._B9._ua._3q);
this._Fm._xr(this._yr)
}
};
In my function (for example) what goes here?
HELPME.fillStyle = "#FF0000";
HELPME.fillRect(0,0,150,75);

Pixastic javascript library is returning false in image tag when i am trying to use it in Windows 8 Metro Style app in javascript?

I am trying to use Pixastic JavaScript library for image manipulation in Windows 8 Metry Style app using JavaScript as language. I am passing canvas as input parameter and trying to get image and binging again to canvas but the image value that i am getting all the time is FALSE.
CODE:
// First, draw it into the backing canvas
context.drawImage(video, 0, 0, width, height);
// change brightness and contrast
Pixastic.Client.isIE = false;
Pixastic.process(canvas, 'brightness',
{
'brightness': 2,
'contrast': 0.0,
'leaveDOM': true
},
function (img) {
if (img) {
context.putImageData(img, 0, 0);
}
}
);
I tried to find the reason and its said that this class returns false only if there is an internal error or some other problem occurs. when i debugged the code its runs fine no issue with my written code. But when i check inside Pixastic library i found that on one condition check its returning false
if(imageIsCanvas&&Pixastic.Client.isIE()){if(Pixastic.debug)writeDebug("Tried to process a canvas element but browser is IE.");return false;
I don't know how can i use Pixastic in my Windows 8 app. Can anyone suggest a way to use pixastic library in Windows 8 app?
It seems that Pixastic doesn't support IE. From their website:
The methods are currently only fully supported by Firefox, Opera and
Safari with a recent WebKit nightly build.
You could change your isIE workaround to either be a function (as below) or change the implementation in the library.
Pixastic.Client.isIE = function(){ return false; };
Though no doubt other things could be broken.

Categories

Resources