Skip to content

Instantly share code, notes, and snippets.

@abicky
Created July 5, 2012 11:00
Show Gist options
  • Save abicky/3052980 to your computer and use it in GitHub Desktop.
Save abicky/3052980 to your computer and use it in GitHub Desktop.
Create HTMLImageElement from HTMLCanvasElement and HTMLCanvasElement from HTMLImageElement
import "js/web.jsx";
class Util {
static function image2Canvas(image : HTMLImageElement) : HTMLCanvasElement {
var document = dom.window.document;
var canvas = document.createElement("canvas") as HTMLCanvasElement;
canvas.width = image.width;
canvas.height = image.height;
var ctx = canvas.getContext("2d") as CanvasRenderingContext2D;
ctx.drawImage(image, 0, 0);
return canvas;
}
static function canvas2PNGImage(canvas : HTMLCanvasElement) : HTMLImageElement {
var document = dom.window.document;
var image = document.createElement("img") as HTMLImageElement;
image.src = canvas.toDataURL();
return image;
}
static function canvas2JPEGImage(canvas : HTMLCanvasElement) : HTMLImageElement {
var document = dom.window.document;
var image = document.createElement("img") as HTMLImageElement;
image.src = canvas.toDataURL("image/jpeg");
return image;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment