Skip to content

Instantly share code, notes, and snippets.

@RafaelOliveira
Created March 24, 2017 19:01
Show Gist options
  • Save RafaelOliveira/050de54121b38b1945fb71624f258d3d to your computer and use it in GitHub Desktop.
Save RafaelOliveira/050de54121b38b1945fb71624f258d3d to your computer and use it in GitHub Desktop.
Create a screenshot in Kha (only HTML5)
// Ported from https://github.com/BabylonJS/Babylon.js/blob/master/src/Tools/babylon.tools.ts
// Don't seems to work in KodeStudio, only in a browser.
// It creates a new window with the image. If you can't save the image, try to right click in the image
// and choose to open in a new tab.
#if js
package;
import js.html.Window;
import js.html.CanvasElement;
import js.html.ImageElement;
class Screenshot
{
public static function create(mimeType:String = 'image/png'):Void
{
var canvas = kha.SystemImpl.khanvas;
var screenshotCanvas:CanvasElement = cast js.Browser.document.createElement('canvas');
screenshotCanvas.width = canvas.width;
screenshotCanvas.height = canvas.height;
var renderContext = screenshotCanvas.getContext('2d');
renderContext.drawImage(canvas, 0, 0, canvas.width, canvas.height);
var base64Image = screenshotCanvas.toDataURL(mimeType);
var newWindow = js.Browser.window.open(null, '_blank');
adjustWindow(newWindow);
var img:ImageElement = cast newWindow.document.createElement('img');
img.src = base64Image;
newWindow.document.body.appendChild(img);
}
static function adjustWindow(window:Window):Void
{
window.document.body.style.margin = '0px';
window.document.body.style.padding = '0px';
window.document.body.style.height = '100%';
window.document.body.style.overflow = 'hidden';
window.document.documentElement.style.margin = '0px';
window.document.documentElement.style.padding = '0px';
window.document.documentElement.style.height = '100%';
window.document.documentElement.style.overflow = 'hidden';
}
}
#end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment