I found a pc to test webgl on, and it seems that you can't get both a webgl and 2d context on the same canvas. That explains why my code doesn't work.
You can still save the entire canvas. Ex:
var canvas = document.getElementById( 'c2canvas' );
canvas.toDataURL("image/png")
or
canvas.toDataURL("image/jpeg", 0.75)
That is all the runtime is doing, except it waits till after everything is drawn for that frame.
From a plugin you can get the canvas with:
this.runtime.canvas
instead of
document.getElementById( 'c2canvas' );
So the process to get the color of a pixel that also works with webgl would be
var img = new Image();
var self = this;
img.onload = function ()
{
var canvas = document.createElement('canvas');
canvas.width = img.width;
canvas.height = img.height;
var ctx = canvas.getContext('2d');
ctx.drawImage(img,0,0);
var pixel = ctx.getImageData( x , y , 1 , 1 );
// save the value in the plugin and call a trigger to use the color in your program
self.redvalue=pixel.data[ 0 ];
self.runtime.trigger(cr.plugins_.Sprite.prototype.cnds.OnURLLoaded, self);
}
img["crossOrigin"] = "anonymous";
img.src = this.runtime.canvas.toDataURL("image/png")[/code:15nrlz3e]
It's referenced from the loadUrl sprite action.
Basically load toDataUrl to a image. This is asynchronous so we need a callback when it finishes. When it's done loading we can then create a canvas, draw the image to it and retrieve a pixel with a 2d context. Then we save the pixel to a variable we can access from an expression and call a event sheet trigger.
Whew, that's not exactly straightforward. When webgl is off a pixel can be retrieved in place with the code in your op.
Now to go the webgl route instead there's a nifty readpixels function so supposedly it's as simple as this:
[url=https://developer.mozilla.org/en-US/docs/Web/API/WebGLRenderingContext/readPixels]https://developer.mozilla.org/en-US/doc ... readPixels[/url]
[code:15nrlz3e]var canvas=document.getElementById('c2canvas');
var gl=canvas.c2runtime.gl;
var pixel = new Uint8Array(4);
gl.readPixels(0, 0, 1, 1, gl.RGBA, gl.UNSIGNED_BYTE, pixel);
pixel[0];[/code:15nrlz3e]
which works only on frame 1 it seems. I don't know why, and my google fu hasn't been helped me yet. But that could be a start.
Edit:
Here's the proper solution:
[url=https://asalga.wordpress.com/2011/08/01/using-webgl-readpixels-turn-on-preservedrawingbuffer/]https://asalga.wordpress.com/2011/08/01 ... ingbuffer/[/url]
But that requires modifying the runtime in preview.js.