Advertisement
Guest User

The JavaScript code

a guest
Jun 12th, 2014
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function isNumber(evt) {
  2.     evt = (evt) ? evt : window.event;
  3.     var charCode = (evt.which) ? evt.which : evt.keyCode;
  4.     if (charCode > 31 && (charCode < 48 || charCode > 57)) {
  5.         return false;
  6.     }
  7.     return true;
  8. }
  9.  
  10. function Collage(canvasId) {
  11.     var NONE = 0, MOVING = 1, SCALING = 2, ROTATING = 3;
  12.    
  13.     var mouseDown = false;
  14.     var mousePrevX = 0;
  15.     var mousePrevY = 0;
  16.    
  17.     var selectedLayer = null;
  18.     var layerState = NONE;
  19.    
  20.     var backgroundColor = null;
  21.     var backgroundImage = null;
  22.    
  23.     var canvasOffsetX = 0;
  24.     var canvasOffsetY = 0;
  25.    
  26.     var scaleImg = new Image();
  27.     var rotateImg = new Image();
  28.     scaleImg.src = "resize.png";
  29.     rotateImg.src = "rotate.png";
  30.    
  31.     var canvas = $(canvasId)[0];
  32.    
  33.     canvasOffsetX = $(canvasId).offset().left;
  34.     canvasOffsetY = $(canvasId).offset().top;
  35.    
  36.     var context = canvas.getContext('2d');
  37.     var layers = new Array();
  38.    
  39.     canvas.addEventListener('mousemove', mouseMoveEvent, false);
  40.     canvas.addEventListener('mouseout', mouseOutEvent, false);
  41.     canvas.addEventListener('mouseup', mouseOutEvent, false);
  42.     canvas.addEventListener('mouseleave', mouseOutEvent, false);
  43.     canvas.addEventListener('mousedown', mouseDownEvent, false);
  44.  
  45.    
  46.    
  47.     this.addLayer = function(img) {
  48.         var layer = new Layer(img);
  49.         layers.push(layer);
  50.        
  51.         redrawCanvas();
  52.         return layer;
  53.     }
  54.     this.addTextLayer = function(content, fontFamily, fontSize, fontColor) {
  55.         var layer = new TextLayer(content, fontFamily, fontSize, fontColor);
  56.         layer.setTitle(content);
  57.         layers.push(layer);
  58.        
  59.         redrawCanvas();
  60.        
  61.         return layer;
  62.     }
  63.     this.redraw = function() {
  64.         redrawCanvas();
  65.     }
  66.     this.getCanvas = function() {
  67.         return canvas;
  68.     }
  69.     this.setBackgroundImage = function(img) {
  70.         backgroundImage = img;
  71.         redrawCanvas();
  72.     }
  73.     this.setBackgroundColor = function(color) {
  74.         backgroundColor = color;
  75.         redrawCanvas();
  76.     }
  77.    
  78.     this.getLayers = function() {
  79.         return layers;
  80.     }
  81.     this.getLayer = function(i) {
  82.         return layers[i];
  83.     }
  84.     this.moveLayerUp = function(i) {
  85.         i = parseInt(i);
  86.         if (i < layers.length - 1) {
  87.             var swapLayer = layers[1 + i];
  88.             layers[1 + i] = layers[i];
  89.             layers[i] = swapLayer;
  90.             redrawCanvas();
  91.             return true;
  92.         } else {
  93.             return false;
  94.         }
  95.     }
  96.     this.moveLayerDown = function(i) {
  97.         i = parseInt(i);
  98.         if (i > 0) {
  99.             var swapLayer = layers[i - 1];
  100.             layers[i - 1] = layers[i];
  101.             layers[i] = swapLayer;
  102.             redrawCanvas();
  103.             return true;
  104.         } else {
  105.             return false;
  106.         }
  107.     }
  108.     this.removeLayer = function(i) {
  109.         i = parseInt(i);
  110.         if (i >= 0 && i < layers.length) {
  111.             layers.splice(i,1);
  112.             redrawCanvas();
  113.         }
  114.     }
  115.    
  116.     function redrawCanvas() {
  117.         if (backgroundImage != null) {
  118.             context.drawImage(backgroundImage, 0, 0, canvas.width, canvas.height);
  119.         } else if (backgroundColor != null && backgroundColor != "") {
  120.             context.fillStyle = backgroundColor;
  121.             context.fillRect(0, 0, canvas.width, canvas.height);
  122.         } else {
  123.             context.clearRect(0, 0, canvas.width, canvas.height);
  124.         }
  125.         for (i in layers) {
  126.             if (layers[i].isVisible()) {
  127.                 context.save();
  128.                
  129.                 context.globalAlpha = layers[i].getOpacity();
  130.                 context.globalCompositeOperation = layers[i].getCompositeOperation();
  131.                
  132.                 if (layers[i].hasShadow()) {
  133.                     context.shadowColor = "#000";
  134.                     context.shadowOffsetX = 3;
  135.                     context.shadowOffsetY = 3;
  136.                     context.shadowBlur = 15;
  137.                 }
  138.                
  139.                 context.translate(layers[i].offsetX + (layers[i].width / 2), layers[i].offsetY + (layers[i].height / 2));
  140.                 context.rotate(layers[i].getAngle());
  141.                
  142.                 context.drawImage(layers[i].getCanvas(), 0 - (layers[i].width / 2), 0 - (layers[i].height / 2));
  143.                
  144.                 context.restore();
  145.             }
  146.         }
  147.     }
  148.    
  149.     function drawMarker(layer) {
  150.         redrawCanvas();
  151.        
  152.         context.save();
  153.         context.lineWidth=1;
  154.         context.strokeStyle = "#000000";
  155.         context.setLineDash([4]);
  156.         context.globalAlpha = 0.75;
  157.        
  158.         context.translate(layer.offsetX + (layer.width / 2), layer.offsetY + (layer.height / 2));
  159.         context.rotate(layer.getAngle());
  160.        
  161.         context.strokeRect(0 - (layer.width / 2), 0 - (layer.height / 2),
  162.             layer.width, layer.height);
  163.        
  164.         if (layer instanceof Layer) {
  165.             context.drawImage(scaleImg,
  166.                     (layer.width / 2) - scaleImg.width,
  167.                     (layer.height / 2) - scaleImg.height);
  168.         }
  169.                
  170.         context.drawImage(rotateImg,
  171.                 (layer.width / 2) - rotateImg.width,
  172.                 0 - (layer.height / 2));
  173.                
  174.         document.getElementById('width').value = Math.round(layer.width*100)/100;
  175.         document.getElementById('height').value = Math.round(layer.height*100)/100;
  176.         document.getElementById('x').value = Math.round(layer.offsetX*100)/100;
  177.         document.getElementById('y').value = Math.round(layer.offsetY*100)/100;
  178.         document.getElementById('angle').value = Math.round(layer.angle*100)/100;
  179.         setElementValueById('font', layer.fontFamily);
  180.         setElementValueById('size', layer.fontSize);
  181.         setElementValueById('color', layer.fontColor);
  182.         setElementValueById('content', layer.content);
  183.    
  184.         context.restore();
  185.     }
  186.    
  187.     //Als een textbox de waarde 'undefined' heeft dan wordt de <div> waar de textbox in zit verborgen
  188.     function setElementValueById(id, value){
  189.     var element = document.getElementById(id);
  190.         if(element != null){
  191.             element.parentElement.style.display = value !== undefined ? '': 'none';
  192.             element.value = value;
  193.         }
  194.     }
  195.    
  196.     function mouseOutEvent(e) {
  197.         mouseDown = false;
  198.         layerState = NONE;
  199.     }
  200.    
  201.     function mouseDownEvent(e) {
  202.         mouseDown = true;
  203.     }
  204.  
  205.     document.getElementById('content').onchange=function(){
  206.         selectedLayer.width = selectedLayer.context.measureText(document.getElementById('content').value).width;
  207.         selectedLayer.content = document.getElementById('content').value;
  208.         drawMarker(selectedLayer);
  209.     };
  210.    
  211.     document.getElementById('angle').onchange=function(){
  212.         selectedLayer.setAngle(document.getElementById('angle').value);
  213.         drawMarker(selectedLayer);
  214.     };
  215.    
  216.     document.getElementById('x').onchange=function(){
  217.         selectedLayer.offsetX = document.getElementById('x').value - canvasOffsetX + 360;
  218.         drawMarker(selectedLayer);
  219.     };
  220.    
  221.     document.getElementById('y').onchange=function(){
  222.         selectedLayer.offsetY = document.getElementById('y').value - canvasOffsetY + 101.91;
  223.         drawMarker(selectedLayer);
  224.     };
  225.     /*
  226.     document.getElementById('width').onchange=function(){
  227.         selectedLayer.width = document.getElementById('width').value;
  228.         drawMarker(selectedLayer);
  229.     };
  230.  
  231.     document.getElementById('height').onchange=function(){
  232.         selectedLayer.height = document.getElementById('height').value;
  233.         drawMarker(selectedLayer);
  234.     };
  235.     */
  236.     function mouseMoveEvent(e) {
  237.         if (layerState == SCALING && mouseDown) {
  238.             var square = selectedLayer.getSquare();
  239.             var scaleToWidth = Math.sqrt(Math.pow(square.d.x - (e.pageX - canvasOffsetX), 2) + Math.pow(square.d.y - (e.pageY - canvasOffsetY), 2));
  240.             var scaleToHeight = Math.sqrt(Math.pow(square.b.x - (e.pageX - canvasOffsetX), 2) + Math.pow(square.b.y - (e.pageY - canvasOffsetY), 2));
  241.            
  242.             selectedLayer.scale(scaleToWidth, scaleToHeight);
  243.             drawMarker(selectedLayer);
  244.        
  245.            
  246.         } else if (layerState == ROTATING && mouseDown) {
  247.             var originX = selectedLayer.offsetX + (selectedLayer.width / 2);
  248.             var originY = selectedLayer.offsetY + (selectedLayer.height / 2);
  249.             var mouseX = e.pageX - canvasOffsetX;
  250.             var mouseY = e.pageY - canvasOffsetY;
  251.                
  252.             var angle = Math.atan2((mouseY - originY), (mouseX - originX));
  253.             angle = angle + (Math.PI / 4);
  254.                
  255.             selectedLayer.setAngle(angle);
  256.  
  257.             drawMarker(selectedLayer);
  258.            
  259.         } else if (layerState == MOVING && mouseDown) {
  260.             selectedLayer.offsetX += e.pageX - canvasOffsetX - mousePrevX;
  261.             selectedLayer.offsetY += e.pageY - canvasOffsetY - mousePrevY;
  262.            
  263.             drawMarker(selectedLayer);
  264.            
  265.         } else if (layers.length > 0) {
  266.             var intersected = false;
  267.             for (var i = layers.length; i--; i >= 0) {
  268.                 if (layers[i].intersect(e.pageX - canvasOffsetX, e.pageY - canvasOffsetY) && layers[i].isVisible()) {
  269.                     intersected = true;
  270.                     selectedLayer = layers[i];
  271.                
  272.                     if (isScalingArea(e.pageX - canvasOffsetX, e.pageY - canvasOffsetY, layers[i])) {
  273.                         layerState = SCALING;
  274.                        
  275.                     } else if (isRotatingArea(e.pageX - canvasOffsetX, e.pageY - canvasOffsetY, layers[i])) {
  276.                         layerState = ROTATING;
  277.                        
  278.                     } else {
  279.                         layerState = MOVING;
  280.                     }
  281.                
  282.                     drawMarker(layers[i]);
  283.                        
  284.                     break;
  285.                 }
  286.             }
  287.             if (!intersected) {
  288.                 redrawCanvas();
  289.             }
  290.         }
  291.         mousePrevX = e.pageX - canvasOffsetX;
  292.         mousePrevY = e.pageY - canvasOffsetY;
  293.     }
  294.    
  295.     function isScalingArea(mX, mY, layer) {
  296.         if (layer instanceof TextLayer) {
  297.             return false;
  298.         }
  299.        
  300.         var scaleOffsetX = layer.offsetX + layer.width;
  301.         var scaleOffsetY = layer.offsetY + layer.height;
  302.        
  303.         var square = new Square(
  304.                 new Vector(scaleOffsetX - scaleImg.width, scaleOffsetY - scaleImg.height),
  305.                 new Vector(scaleOffsetX, scaleOffsetY - scaleImg.height),
  306.                 new Vector(scaleOffsetX, scaleOffsetY),
  307.                 new Vector(scaleOffsetX - scaleImg.width, scaleOffsetY));
  308.  
  309.         square.rotate(layer.angle);
  310.         square.alignBottomRight(layer.getSquare().c);
  311.            
  312.         return square.intersect(new Vector(mX, mY));
  313.     }
  314.    
  315.     function isRotatingArea(mX, mY, layer) {
  316.         var scaleOffsetX = layer.offsetX + layer.width;
  317.         var scaleOffsetY = layer.offsetY + layer.height;
  318.        
  319.         var square = new Square(
  320.                 new Vector(scaleOffsetX - rotateImg.width, layer.offsetY),
  321.                 new Vector(scaleOffsetX, layer.offsetY),
  322.                 new Vector(scaleOffsetX, layer.offsetY + rotateImg.height),
  323.                 new Vector(scaleOffsetX - rotateImg.width, layer.offsetY + rotateImg.height));
  324.  
  325.         square.rotate(layer.angle);
  326.         square.alignTopRight(layer.getSquare().b);
  327.            
  328.         return square.intersect(new Vector(mX, mY));
  329.     }
  330. }
  331.  
  332. function BaseLayer() {
  333.     this.offsetX = 0;
  334.     this.offsetY = 0;
  335.  
  336.     this.opacity = 1;
  337.     this.visible = true;
  338.     this.shadow = false;
  339.     this.compositeOperation = "source-over";
  340.     this.angle = 0;
  341.     this.title = "";
  342.    
  343.     this.isVisible = function() {
  344.         return this.visible;
  345.     }
  346.     this.toggleVisible = function() {
  347.         this.visible = !this.visible;
  348.     }
  349.    
  350.     this.hasShadow = function() {
  351.         return this.shadow;
  352.     }
  353.     this.toggleShadow = function() {
  354.         this.shadow = !this.shadow;
  355.     }
  356.     this.setShadow = function(shadow) {
  357.         this.shadow = shadow;
  358.     }
  359.    
  360.     this.getOpacity = function() {
  361.         return this.opacity;
  362.     }
  363.     this.setOpacity = function(opacity) {
  364.         this.opacity = opacity;
  365.     }
  366.    
  367.     this.setCompositeOperation = function(compositeOperation) {
  368.         this.compositeOperation = compositeOperation;
  369.     }
  370.     this.getCompositeOperation = function() {
  371.         return this.compositeOperation;
  372.     }
  373.    
  374.     this.getCanvas = function() {
  375.         this.redraw();
  376.         return this.canvas;
  377.     }
  378.    
  379.     this.intersect = function(x, y) {
  380.  
  381.         var square = new Square(
  382.                 new Vector(this.offsetX, this.offsetY),
  383.                 new Vector(this.offsetX + this.width, this.offsetY),
  384.                 new Vector(this.offsetX + this.width, this.offsetY + this.height),
  385.                 new Vector(this.offsetX, this.offsetY + this.height));
  386.        
  387.         square.rotate(this.angle);
  388.        
  389.         return square.intersect(new Vector(x, y));
  390.     }
  391.    
  392.     this.getSquare = function() {
  393.         var square = new Square(
  394.                 new Vector(this.offsetX, this.offsetY),
  395.                 new Vector(this.offsetX + this.width, this.offsetY),
  396.                 new Vector(this.offsetX + this.width, this.offsetY + this.height),
  397.                 new Vector(this.offsetX, this.offsetY + this.height));
  398.                
  399.         square.rotate(this.angle);
  400.        
  401.         return square;
  402.     }
  403.    
  404.     this.scale = function(width, height) {
  405.         this.context.restore();
  406.        
  407.         this.width = width;
  408.         this.height = height;
  409.         this.canvas.width = width;
  410.         this.canvas.height = height;
  411.        
  412.         this.context.translate(this.width / 2, this.height / 2);
  413.        
  414.         this.redraw();
  415.     }
  416.    
  417.     this.setAngle = function(angle) {
  418.         this.angle = angle;
  419.     }
  420.     this.getAngle = function() {
  421.         return this.angle;
  422.     }
  423.    
  424.     this.setTitle = function(title) {
  425.         this.title = title;
  426.     }
  427.     this.getTitle = function() {
  428.         return this.title;
  429.     }
  430.    
  431.     this.redraw = function() {
  432.         var startX = this.width / 2 - this.width;
  433.         var startY = this.height / 2 - this.height;
  434.        
  435.     }
  436. };
  437.  
  438. function Layer(img) {
  439.     this.img = img;
  440.    
  441.     this.width = img.naturalWidth;
  442.     this.height = img.naturalHeight;
  443.    
  444.     this.canvas = document.createElement('canvas');
  445.     this.canvas.width = this.width;
  446.     this.canvas.height = this.height;
  447.     this.context = this.canvas.getContext('2d');
  448.     this.context.save();
  449.     this.context.translate(this.width / 2, this.height / 2);
  450.    
  451.     this.getImage = function() {
  452.         return this.img;
  453.     }
  454.    
  455.     this.redraw = function() {
  456.         var startX = this.width / 2 - this.width;
  457.         var startY = this.height / 2 - this.height;
  458.        
  459.         this.context.clearRect(startX, startY, this.canvas.width, this.canvas.height);
  460.         this.context.drawImage(this.img, startX, startY, this.width, this.height);
  461.     }
  462. };
  463. Layer.prototype = new BaseLayer();
  464.  
  465. function TextLayer(content, fontFamily, fontSize, fontColor) {
  466.     this.content = content;
  467.     this.fontFamily = fontFamily;
  468.     this.fontSize = fontSize;
  469.     this.fontColor = fontColor;
  470.    
  471.     this.canvas = document.createElement('canvas');
  472.     this.context = this.canvas.getContext('2d');
  473.    
  474.     this.context.font = this.fontSize + 'px ' + this.fontFamily;
  475.     this.context.fillStyle = this.fontColor;
  476.     this.context.textBaseline = "top";
  477.    
  478.     this.width = this.context.measureText(content).width;
  479.     this.height = fontSize + (fontSize / 4);
  480.    
  481.     this.canvas.width = this.width;
  482.     this.canvas.height = this.height;
  483.    
  484.     this.context.save();
  485.     this.context.translate(this.width / 2, this.height / 2);
  486.    
  487.     this.redraw = function() {
  488.         var startX = this.width / 2 - this.width;
  489.         var startY = this.height / 2 - this.height;
  490.        
  491.         this.context.font = this.fontSize + 'px ' + this.fontFamily;
  492.         this.context.fillStyle = this.fontColor;
  493.         this.context.textBaseline = "top";
  494.        
  495.         this.context.clearRect(startX, startY, this.canvas.width, this.canvas.height);
  496.         //this.context.clearRect(0,0, this.canvas.width, this.canvas.height);
  497.        
  498.         this.context.fillText(this.content, startX, startY);
  499.     }
  500. };
  501. TextLayer.prototype = new BaseLayer();
  502.  
  503. function Square(a, b, c, d) {
  504.     this.a = a;
  505.     this.b = b;
  506.     this.c = c;
  507.     this.d = d;
  508.     this.origin = centerSquareOrigin(a,b,c,d);
  509.    
  510.     this.intersect = function(mouse) {
  511.         return (!intersectWithLine(this.origin, mouse, this.a, this.b) &&
  512.             !intersectWithLine(this.origin, mouse, this.b, this.c) &&
  513.             !intersectWithLine(this.origin, mouse, this.c, this.d) &&
  514.             !intersectWithLine(this.origin, mouse, this.d, this.a));
  515.     }
  516.    
  517.     this.rotate = function(angle) {
  518.         var radius = Math.sqrt(Math.pow(this.origin.x - this.a.x, 2) + Math.pow(this.origin.y - this.a.y, 2));
  519.        
  520.         var aAngle = Math.atan2((this.a.y - this.origin.y), (this.a.x - this.origin.x));
  521.         var bAngle = Math.atan2((this.b.y - this.origin.y), (this.b.x - this.origin.x));
  522.         var cAngle = Math.atan2((this.c.y - this.origin.y), (this.c.x - this.origin.x));
  523.         var dAngle = Math.atan2((this.d.y - this.origin.y), (this.d.x - this.origin.x));
  524.        
  525.         this.a.x = this.origin.x + radius * Math.cos(angle + aAngle);
  526.         this.a.y = this.origin.y + radius * Math.sin(angle + aAngle);
  527.         this.b.x = this.origin.x + radius * Math.cos(angle + bAngle);
  528.         this.b.y = this.origin.y + radius * Math.sin(angle + bAngle);
  529.         this.c.x = this.origin.x + radius * Math.cos(angle + cAngle);
  530.         this.c.y = this.origin.y + radius * Math.sin(angle + cAngle);
  531.         this.d.x = this.origin.x + radius * Math.cos(angle + dAngle);
  532.         this.d.y = this.origin.y + radius * Math.sin(angle + dAngle);
  533.     }
  534.    
  535.     this.alignBottomRight = function(alignPoint) {
  536.         var diff = new Vector(alignPoint.x - this.c.x, alignPoint.y - this.c.y);
  537.        
  538.         this.a = this.a.add(diff);
  539.         this.b = this.b.add(diff);
  540.         this.c = this.c.add(diff);
  541.         this.d = this.d.add(diff);
  542.         this.origin = centerSquareOrigin(this.a, this.b, this.c, this.d);
  543.     }
  544.    
  545.     this.alignTopRight = function(alignPoint) {
  546.         var diff = new Vector(alignPoint.x - this.b.x, alignPoint.y - this.b.y);
  547.        
  548.         this.a = this.a.add(diff);
  549.         this.b = this.b.add(diff);
  550.         this.c = this.c.add(diff);
  551.         this.d = this.d.add(diff);
  552.         this.origin = centerSquareOrigin(this.a, this.b, this.c, this.d);
  553.     }
  554.    
  555.     var epsilon = 10e-6;
  556.    
  557.     function centerSquareOrigin(a, b, c, d) {
  558.         p = a;
  559.         r = c.subtract(a);
  560.         q = b;
  561.         s = d.subtract(b);
  562.        
  563.         rCrossS = cross(r, s);
  564.         if(rCrossS <= epsilon && rCrossS >= -1 * epsilon){
  565.             return;
  566.         }
  567.         t = cross(q.subtract(p), s)/rCrossS;
  568.         u = cross(q.subtract(p), r)/rCrossS;
  569.         if (0 <= u && u <= 1 && 0 <= t && t <= 1){
  570.             intPoint = p.add(r.scalarMult(t));
  571.             return new Vector(intPoint.x, intPoint.y);
  572.         }
  573.        
  574.         return null;
  575.     }
  576.    
  577.     function cross(v1, v2) {
  578.         return v1.x * v2.y - v2.x * v1.y;
  579.     }
  580.    
  581.     function intersectWithLine(l1p1, l1p2, l2p1, l2p2) {
  582.         p = l1p1;
  583.         r = l1p2.subtract(l1p1);
  584.         q = l2p1;
  585.         s = l2p2.subtract(l2p1);
  586.        
  587.         rCrossS = cross(r, s);
  588.         if(rCrossS <= epsilon && rCrossS >= -1 * epsilon){
  589.             return false;
  590.         }
  591.        
  592.         t = cross(q.subtract(p), s)/rCrossS;
  593.         u = cross(q.subtract(p), r)/rCrossS;
  594.         if(0 <= u && u <= 1 && 0 <= t && t <= 1){
  595.             return true;
  596.         } else{
  597.             return false;
  598.         }
  599.     }
  600. }
  601.  
  602. function Vector(x, y) {
  603.     this.x = x;
  604.     this.y = y;
  605.  
  606.     this.scalarMult = function(scalar){
  607.         return new Vector(this.x * scalar, this.y * scalar);
  608.     }
  609.     this.dot = function(v2) {
  610.         return this.x * v2.x + this.y * v2.y;
  611.     };
  612.     this.perp = function() {
  613.         return new Vector(-1 * this.y, this.x);
  614.     };
  615.     this.subtract = function(v2) {
  616.         return this.add(v2.scalarMult(-1));//new Vector(this.x - v2.x, this.y - v2.y);
  617.     };
  618.     this.add = function(v2) {
  619.         return new Vector(this.x + v2.x, this.y + v2.y);
  620.     }
  621. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement