Skip to content

Instantly share code, notes, and snippets.

@fredludlow
Last active March 21, 2017 14:31
Show Gist options
  • Save fredludlow/bcc70794d43350c219b2a9b94750b8b7 to your computer and use it in GitHub Desktop.
Save fredludlow/bcc70794d43350c219b2a9b94750b8b7 to your computer and use it in GitHub Desktop.
Thumbnail method for NGL
// These functions pulled out of prototype method, `this` has `stage` attribute
{
/** Returns a promise that resolves to an Image object/element */
getImage: function( params ) {
params = Object.assign( {
trim: false,
antialias: true,
transparent: false }, params );
return this.stage.makeImage(params).then( function( blob ){
return new Promise( function( resolve, reject ){
var fr = new FileReader();
var image = new Image();
image.onload = function() { resolve( this ); }
fr.onload = function() {
image.src = this.result;
}
fr.readAsDataURL( blob );
});
});
},
// Pulled out of prototype, assumes `this` has `stage`
getThumbnail2: function() {
// Current image
var currentImage = this.getImage();
var self = this;
var viewer = this.stage.viewer;
var canvas = viewer.renderer.domElement;
var container = canvas.parentElement;
var image;
var cameraState = this.getCameraState();
var scale = 100 / viewer.height;
var scaledState = { z: cameraState.z / scale,
zoom: cameraState.zoom * scale };
var restore = function() {
image && image.remove();
self.setCameraState( cameraState );
canvas.style.visibility = "visible";
viewer.handleResize();
}
var thumbnailPromise = currentImage.then( function( img ){
image = img;
img.style.position = canvas.style.position = "absolute";
img.style.top = canvas.style.top = img.style.left = canvas.style.left = "0px";
img.style["z-index"] = 10;
container.appendChild(img);
canvas.style.visibility = "hidden";
viewer.setSize( 100, 100 );
self.setCameraState( scaledState );
viewer.requestRender();
return self.getImage();
});
thumbnailPromise.then( restore, restore );
return thumbnailPromise;
},
getCameraState: function() {
var camera = this.stage.viewer.camera;
return {
z: camera.position.z,
zoom: camera.zoom
};
},
setCameraState: function( p ){
var camera = this.stage.viewer.camera;
camera.position.z = p.z;
camera.zoom = p.zoom;
},
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment