'use strict'; // http://cssdeck.com/labs/pure-css-animated-isometric-boxes try{ var Snap = require('snapsvg'); } catch(e) {}; var SvgCube = function (options) { // Inputs and options var defaultOptions = { angle: 30, size: 64, verbose: false, // outline drawOutline: true, drawShading: true, clipCircle: false, stroke: { "arrow-end": 'none', "stroke": 'black', // stroke color for outline "stroke-width": Math.sqrt(options.size)/2, // outline width "stroke-linecap": "round", "stroke-linejoin": "round", "fill": "none", }, // cube flatten: 0, // fraction to vertically flatten the cube // scale Y 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.1, rightUrl: '', rightRot: 0, rightShad: 0.3, backUrl: '', backRot: 0, backShad: 0.3, bottomUrl: '', bottomRot: 0, bottomShad: 0.3, svgNS: "http://www.w3.org/2000/svg", padding: 0, rotateX: 45, rotateY: 45, rotateZ: 0, perspective: 0, scaleX: 1.00, scaleZ: 1.00, } // 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 if (!options.drawOutline){ options.stroke['stroke-width']=0; } this.init(); } SvgCube.prototype.init = function(){ this.angle = this.options.angle this.w = this.options.size; // input image width this.h = this.options.size; this.f = this.options.flatten this.rot = this.angle * Math.PI / 180 this.padding = this.options.padding; // pading fraction this.cw = this.w; // we will keep same width but change height this.ch = (1 + this.padding) * (this.h / 2 + this.h * Math.tan(this.rot)) -this.h/2*(1-this.f); //canvas height full // create SVG element var o = this.options; //var style = document.createElement('style'); //this.paper.defs.appendChild(style) var styleStr = ` ` //style.innerHTML= 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); } $('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', // }) } // svg2png SvgCube.prototype.toPNG = function(){ try{ var dataUrl = svg2png(this.paper.node); img = document.createElement("img"); document.body.appendChild(img) img.src=dataUrl img.id="toPNG" } catch (e) {console.log(e)}; } // svg2png SvgCube.prototype.toPNG2 = function(){ var dataUrl = svg2png(this.paper.node); img = document.createElement("img"); document.body.appendChild(img) img.src=dataUrl img.id="toPNG" } // svg2png SvgCube.prototype.update = function(){ //this.paper.remove(); this.init(); this.drawCube(); } try{ module.exports=SvgCube } catch(e){};