FIX: bezier signature upload image
[roojs1] / Roo / bootstrap / BezierSignature.js
index 504e13b..379c181 100644 (file)
 
 Roo.bootstrap.BezierSignature = function(config){
     Roo.bootstrap.BezierSignature.superclass.constructor.call(this, config);
+    this.addEvents({
+        "resize" : true
+    });
 };
 
 Roo.extend(Roo.bootstrap.BezierSignature, Roo.bootstrap.Component,  {
     
-    _data: [],
+    curve_data: [],
     
-    _isEmpty: true,
+    is_empty: true,
     
-    _mouseButtonDown: true,
+    mouse_btn_down: true,
+    
+    /**
+     * @cfg(int) canvas height
+     */
+    canvas_height: '200px',
     
     /**
      * @cfg(float or function) Radius of a single dot.
      */ 
-    dotSize: false,
+    dot_size: false,
     
     /**
      * @cfg(float) Minimum width of a line. Defaults to 0.5.
      */
-    minWidth: 0.5,
+    min_width: 0.5,
     
     /**
      * @cfg(float) Maximum width of a line. Defaults to 2.5.
      */
-    maxWidth: 2.5,
+    max_width: 2.5,
     
     /**
      * @cfg(integer) Draw the next point at most once per every x milliseconds. Set it to 0 to turn off throttling. Defaults to 16.
@@ -50,22 +58,22 @@ Roo.extend(Roo.bootstrap.BezierSignature, Roo.bootstrap.Component,  {
     /**
      * @cfg(integer) Add the next point only if the previous one is farther than x pixels. Defaults to 5.
      */
-    minDistance: 5,
+    min_distance: 5,
     
     /**
      * @cfg(string) Color used to clear the background. Can be any color format accepted by context.fillStyle. Defaults to "rgba(0,0,0,0)" (transparent black). Use a non-transparent color e.g. "rgb(255,255,255)" (opaque white) if you'd like to save signatures as JPEG images.
      */
-    backgroundColor: 'rgba(0, 0, 0, 0)',
+    bg_color: 'rgba(0, 0, 0, 0)',
     
     /**
      * @cfg(string) Color used to draw the lines. Can be any color format accepted by context.fillStyle. Defaults to "black".
      */
-    penColor: 'black',
+    dot_color: 'black',
     
     /**
      * @cfg(float) Weight used to modify new velocity based on the previous velocity. Defaults to 0.7.
      */
-    velocityFilterWeight: 0.7,
+    velocity_filter_weight: 0.7,
     
     /**
      * @cfg(function) Callback when stroke begin.
@@ -79,12 +87,25 @@ Roo.extend(Roo.bootstrap.BezierSignature, Roo.bootstrap.Component,  {
     
     getAutoCreate : function()
     {
-        var cls = 'roo-signature';
+        var cls = 'roo-signature column';
         
         if(this.cls){
             cls += ' ' + this.cls;
         }
         
+        var col_sizes = [
+            'lg',
+            'md',
+            'sm',
+            'xs'
+        ];
+        
+        for(var i = 0; i < col_sizes.length; i++) {
+            if(this[col_sizes[i]]) {
+                cls += " col-"+col_sizes[i]+"-"+this[col_sizes[i]];
+            }
+        }
+        
         var cfg = {
             tag: 'div',
             cls: cls,
@@ -95,9 +116,16 @@ Roo.extend(Roo.bootstrap.BezierSignature, Roo.bootstrap.Component,  {
                     cn: [
                         {
                             tag: 'canvas',
-                            cls: 'roo-signature-body-canvas'
+                            cls: 'roo-signature-body-canvas',
+                            height: this.canvas_height,
+                            width: this.canvas_width
                         }
                     ]
+                },
+                {
+                    tag: 'input',
+                    type: 'file',
+                    style: 'display: none'
                 }
             ]
         };
@@ -115,44 +143,78 @@ Roo.extend(Roo.bootstrap.BezierSignature, Roo.bootstrap.Component,  {
         canvas.dom.style.touchAction = 'none';
         canvas.dom.style.msTouchAction = 'none';
         
-        this._mouseButtonDown = false;
+        this.mouse_btn_down = false;
         canvas.on('mousedown', this._handleMouseDown, this);
         canvas.on('mousemove', this._handleMouseMove, this);
         Roo.select('html').first().on('mouseup', this._handleMouseUp, this);
         
-        if (window.ontouchstart) {
+        if (window.PointerEvent) {
+            canvas.on('pointerdown', this._handleMouseDown, this);
+            canvas.on('pointermove', this._handleMouseMove, this);
+            Roo.select('html').first().on('pointerup', this._handleMouseUp, this);
+        }
+        
+        if ('ontouchstart' in window) {
             canvas.on('touchstart', this._handleTouchStart, this);
             canvas.on('touchmove', this._handleTouchMove, this);
             canvas.on('touchend', this._handleTouchEnd, this);
         }
         
+        Roo.EventManager.onWindowResize(this.resize, this, true);
+        
+        // file input event
+        this.fileEl().on('change', this.uploadImage, this);
+        
         this.clear();
+        
+        this.resize();
+    },
+    
+    resize: function(){
+        
+        var canvas = this.canvasEl().dom;
+        var ctx = this.canvasElCtx();
+        var img_data = ctx.getImageData(0, 0, canvas.width, canvas.height);
+        
+        // setting canvas width will clean img data
+        canvas.width = 0;
+        
+        var style = window.getComputedStyle ? 
+            getComputedStyle(this.el.dom, null) : this.el.dom.currentStyle;
+            
+        var padding_left = parseInt(style.paddingLeft) || 0;
+        var padding_right = parseInt(style.paddingRight) || 0;
+        
+        canvas.width = this.el.dom.clientWidth - padding_left - padding_right;
+        
+        ctx.putImageData(img_data, 0, 0);
     },
     
     _handleMouseDown: function(e)
     {
         if (e.browserEvent.which === 1) {
-            this._mouseButtonDown = true;
+            this.mouse_btn_down = true;
             this.strokeBegin(e);
         }
     },
     
     _handleMouseMove: function (e)
     {
-        if (this._mouseButtonDown) {
+        if (this.mouse_btn_down) {
             this.strokeMoveUpdate(e);
         }
     },
     
     _handleMouseUp: function (e)
     {
-        if (e.browserEvent.which === 1 && this._mouseButtonDown) {
-            this._mouseButtonDown = false;
+        if (e.browserEvent.which === 1 && this.mouse_btn_down) {
+            this.mouse_btn_down = false;
             this.strokeEnd(e);
         }
     },
     
     _handleTouchStart: function (e) {
+        
         e.preventDefault();
         if (e.browserEvent.targetTouches.length === 1) {
             // var touch = e.browserEvent.changedTouches[0];
@@ -166,7 +228,7 @@ Roo.extend(Roo.bootstrap.BezierSignature, Roo.bootstrap.Component,  {
         e.preventDefault();
         // var touch = event.targetTouches[0];
         // _this._strokeMoveUpdate(touch);
-        this._strokeMoveUpdate(e);
+        this.strokeMoveUpdate(e);
     },
     
     _handleTouchEnd: function (e) {
@@ -182,8 +244,8 @@ Roo.extend(Roo.bootstrap.BezierSignature, Roo.bootstrap.Component,  {
     reset: function () {
         this._lastPoints = [];
         this._lastVelocity = 0;
-        this._lastWidth = (this.minWidth + this.maxWidth) / 2;
-        this.canvasElCtx().fillStyle = this.penColor;
+        this._lastWidth = (this.min_width + this.max_width) / 2;
+        this.canvasElCtx().fillStyle = this.dot_color;
     },
     
     strokeMoveUpdate: function(e)
@@ -201,7 +263,7 @@ Roo.extend(Roo.bootstrap.BezierSignature, Roo.bootstrap.Component,  {
     strokeBegin: function(e)
     {
         var newPointGroup = {
-            color: this.penColor,
+            color: this.dot_color,
             points: []
         };
         
@@ -209,7 +271,7 @@ Roo.extend(Roo.bootstrap.BezierSignature, Roo.bootstrap.Component,  {
             this.onBegin(e);
         }
         
-        this._data.push(newPointGroup);
+        this.curve_data.push(newPointGroup);
         this.reset();
         this.strokeUpdate(e);
     },
@@ -218,11 +280,11 @@ Roo.extend(Roo.bootstrap.BezierSignature, Roo.bootstrap.Component,  {
     {
         var rect = this.canvasEl().dom.getBoundingClientRect();
         var point = new this.Point(e.xy[0] - rect.left, e.xy[1] - rect.top, new Date().getTime());
-        var lastPointGroup = this._data[this._data.length - 1];
+        var lastPointGroup = this.curve_data[this.curve_data.length - 1];
         var lastPoints = lastPointGroup.points;
         var lastPoint = lastPoints.length > 0 && lastPoints[lastPoints.length - 1];
         var isLastPointTooClose = lastPoint
-            ? point.distanceTo(lastPoint) <= this.minDistance
+            ? point.distanceTo(lastPoint) <= this.min_distance
             : false;
         var color = lastPointGroup.color;
         if (!lastPoint || !(lastPoint && isLastPointTooClose)) {
@@ -265,10 +327,10 @@ Roo.extend(Roo.bootstrap.BezierSignature, Roo.bootstrap.Component,  {
     },
     
     calculateCurveWidths: function (startPoint, endPoint) {
-        var velocity = this.velocityFilterWeight * endPoint.velocityFrom(startPoint) +
-            (1 - this.velocityFilterWeight) * this._lastVelocity;
+        var velocity = this.velocity_filter_weight * endPoint.velocityFrom(startPoint) +
+            (1 - this.velocity_filter_weight) * this._lastVelocity;
 
-        var newWidth = Math.max(this.maxWidth / (velocity + 1), this.minWidth);
+        var newWidth = Math.max(this.max_width / (velocity + 1), this.min_width);
         var widths = {
             end: newWidth,
             start: this._lastWidth
@@ -282,7 +344,7 @@ Roo.extend(Roo.bootstrap.BezierSignature, Roo.bootstrap.Component,  {
     drawDot: function (_a) {
         var color = _a.color, point = _a.point;
         var ctx = this.canvasElCtx();
-        var width = typeof this.dotSize === 'function' ? this.dotSize() : this.dotSize;
+        var width = typeof this.dot_size === 'function' ? this.dot_size() : this.dot_size;
         ctx.beginPath();
         this.drawCurveSegment(point.x, point.y, width);
         ctx.closePath();
@@ -323,19 +385,24 @@ Roo.extend(Roo.bootstrap.BezierSignature, Roo.bootstrap.Component,  {
         var ctx = this.canvasElCtx();
         ctx.moveTo(x, y);
         ctx.arc(x, y, width, 0, 2 * Math.PI, false);
-        this._isEmpty = false;
+        this.is_empty = false;
     },
     
     clear: function()
     {
         var ctx = this.canvasElCtx();
         var canvas = this.canvasEl().dom;
-        ctx.fillStyle = this.backgroundColor;
+        ctx.fillStyle = this.bg_color;
         ctx.clearRect(0, 0, canvas.width, canvas.height);
         ctx.fillRect(0, 0, canvas.width, canvas.height);
-        this._data = [];
+        this.curve_data = [];
         this.reset();
-        this._isEmpty = true;
+        this.is_empty = true;
+    },
+    
+    fileEl: function()
+    {
+        return  this.el.select('input',true).first();
     },
     
     canvasEl: function()
@@ -350,21 +417,46 @@ Roo.extend(Roo.bootstrap.BezierSignature, Roo.bootstrap.Component,  {
     
     getImage: function(type)
     {
-        if(this._isEmpty) {
+        if(this.is_empty) {
             return false;
         }
         
         // encryption ?
-        return this.canvasEl().dom.toDataURL('image/'+type, false);
+        return this.canvasEl().dom.toDataURL('image/'+type, 1);
     },
     
     drawFromImage: function(img_src)
     {
         var img = new Image();
         
+        img.onload = function(){
+            this.canvasElCtx().drawImage(img, 0, 0);
+        }.bind(this);
+        
         img.src = img_src;
         
-        this.canvasElCtx().drawImage(img, 0, 0);
+        this.is_empty = false;
+    },
+    
+    selectImage: function()
+    {
+        this.fileEl().dom.click();
+    },
+    
+    uploadImage: function(e)
+    {
+        var reader = new FileReader();
+        
+        reader.onload = function(e){
+            var img = new Image();
+            img.onload = function(){
+                this.reset();
+                this.canvasElCtx().drawImage(img, 0, 0);
+            }.bind(this);
+            img.src = e.target.result;
+        }.bind(this);
+        
+        reader.readAsDataURL(e.target.files[0]);
     },
     
     // Bezier Point Constructor