'use strict'; /** * Frontend js file to generate an cube of svg's */ // http://cssdeck.com/labs/pure-css-animated-isometric-boxes // polyfill for bind https://github.com/ariya/phantomjs/issues/11281 if (!Function.prototype.bind) { console.log("Using polyfill of Function.prototype.bind"); Function.prototype.bind = function (oThis) { if (typeof this !== "function") { // closest thing possible to the ECMAScript 5 // internal IsCallable function throw new TypeError("Function.prototype.bind - what is trying to be bound is not callable"); } var aArgs = Array.prototype.slice.call(arguments, 1), fToBind = this, fNOP = function () {}, fBound = function () { return fToBind.apply(this instanceof fNOP && oThis ? this : oThis, aArgs.concat(Array.prototype.slice.call(arguments))); }; fNOP.prototype = this.prototype; fBound.prototype = new fNOP(); return fBound; }; } var SvgCube = function (options) { // Inputs and options var defaultOptions = { rotateX: 30, // isometric angle rotateY: 45, rotateZ: 0, perspective: 0, // doesn't do anything usefull scaleX: 1.00, scaleY: 1.00, // flatten scaleZ: 1.00, // push face size: 256, verbose: false, // outline drawOutline: false, drawShading: false, borderRadius: 0, stroke: { "stroke": 'black', // stroke color for outline "stroke-width": 0, // outline width }, // cube topUrl: '', // url for image in top of cuve topRot: 0, // rotation of top image in degrees topShad: 0, // shading for top leftUrl: '', leftRot: 0, leftShad: 0.0, rightUrl: '', rightRot: 0, rightShad: 0.0, backUrl: '', backRot: 0, backShad: 0.0, bottomUrl: '', bottomRot: 0, bottomShad: 0.0, frontUrl: '', frontRot: 0, frontShad: 0.0, }; // add defaults, 2 levels deep options = options || {}; for (var opt in defaultOptions) { if (defaultOptions.hasOwnProperty(opt) && !options.hasOwnProperty(opt)) { options[opt] = defaultOptions[opt]; } for (var opt2 in defaultOptions[opt]) { if (defaultOptions[opt].hasOwnProperty(opt2) && !options[opt].hasOwnProperty(opt2)) { options[opt][opt2] = defaultOptions[opt][opt2]; } } } this.options = options; // legacy compat if (options.flatten !== undefined) { options.scaleY = 1 - options.flatten; } if (options.angle !== undefined) { options.rotateX = options.angle; } if (!options.drawOutline) { options.stroke['stroke-width'] = 0; } this.init(); }; SvgCube.prototype.init = function () { this.rotateX = this.options.rotateX; this.w = this.options.size; // input image width this.h = this.options.size; this.f = 1 - this.options.scaleY; this.rot = this.rotateX * Math.PI / 180; this.padding = this.options.padding || 0; // pading fraction this.cw = this.w; // we will keep same width but change height this.ch = this.h * 5; //(this.h + Math.sqrt(2)*this.h * Math.tan(this.rot)) - this.h / 2 * (this.f); //canvas height full // create SVG element var o = this.options; // Now we generate some css to put everything in good positions var transitions = 0; var styleStr = '' + ''; if ($('body>.cube').length === 0) { var cube = $('
'); var imageFront = $(''); cube.append(imageFront); var imageL = $(''); cube.append(imageL); var imageRight = $(''); cube.append(imageRight); var imageTop = $(''); cube.append(imageTop); var imageBack = $(''); cube.append(imageBack); var imageBottom = $(''); cube.append(imageBottom); $('body').append(cube); /* TODO these will fill in some color inside in order to allow curved edges without seeing through cube, sues o.stroke.stroke for color */ // var xMid = $(`
`) // cube.append(xMid); // // var yMid = $(`
`) // cube.append(yMid); // // var zMid = $(`
`) // cube.append(zMid); $('body').append(cube); } $('head>#projection').remove(); $('head').append($(styleStr)); }; /* draw cube from urls in options and outline according to options */ SvgCube.prototype.drawCube = function () { // var imgS = this.options.size / Math.sqrt(2); // this.cube = this.paper.g(); // this.cube.attr({ // class: 'cube cube2', // id: "c2notnested", // }); // this.leftImg = this.cube.image(this.options.leftUrl,0,0,imgS,imgS); // this.leftImg.attr({ // class: 'face left', // }) // this.rightImg = this.cube.image(this.options.rightUrl,0,0,imgS,imgS); // this.rightImg.attr({ // class: 'face right', // }) // this.topImg = this.cube.image(this.options.topUrl,0,0,imgS,imgS); // this.topImg.attr({ // class: 'face top', // }) }; /** * Get the bounds of the images for a screenshot * uses $.position to get min,max of each side of the image * @return {[type]} [description] */ SvgCube.prototype.getBounds = function () { var bounds = {}; $('.cube>b').each(function () { var pos = this.getBoundingClientRect(); for (var dim in pos) { if (dim in pos) { // init if (bounds[dim]===undefined) { bounds[dim] = { 'max': pos[dim], 'min': pos[dim] }; } else { // get max and min bounds[dim].max = Math.max(bounds[dim].max, pos[dim]); bounds[dim].min = Math.min(bounds[dim].min, pos[dim]); } } } }); return bounds; }; /** * Get the bounds of the images for a screenshot * uses $.position to get min,max of each side of the image * @return {[type]} [description] */ SvgCube.prototype.getAllBounds = function () { var bounds = {}; $('.cube>b').each(function () { var pos = this.getBoundingClientRect(); for (var dim in pos) { if (dim in pos) { // init if (bounds[dim]===undefined) { bounds[dim] = []; } // get max and min bounds[dim].push(pos[dim]); } } }); return bounds; }; SvgCube.prototype.update = function () { //this.paper.remove(); this.init(); this.drawCube(); console.log(this.getBounds()); };