2017-06-14 176 views
0

我構建了一個小web應用程序,用戶可以在畫布上繪製線條。比方說,我畫了一個圓,我怎樣才能做出選擇每個繪製對象的功能。稍後,我想調整(縮放)當前點擊的對象。我的Javascript函數看起來像這樣到目前爲止,但我不知道如何繼續。JavaScript拖動繪製的畫布元素

所以我需要這個步驟

  • 平局帆布鼠標(這個工程已經)
  • 點擊每個繪圖
  • 的foreach圖紙我想例如創建一個滑塊,更改大小(標度)
var canvasTools = (function() { 

    var CanvasEditor = function CanvasEditor(settings) { 

     var color = settings.color || 'black', 
      width = settings.width || 10; 

     this.activeTool = null; 
     this.canvas = settings.canvas; 
     this.panel = settings.panel; 
     this.panelInput = settings.panelInput; 
     this.pencil = new Tool_pencil(this.canvas, color, width); 

    }; 

    CanvasEditor.prototype = { 

     'constructor': CanvasEditor, 
     'clear': function clear() { 
      this.canvas.getContext('2d').clearRect(0, 0, this.canvas.width, this.canvas.height); 
      return this; 
     }, 
     'init': function init() { 
      this.canvas.addEventListener('mousedown', function downHandler(event) { 
       if (this.activeTool) { 
        this.activeTool.start(event); 
       } 
      }.bind(this), false); 

      this.canvas.addEventListener('mousemove', function downHandler(event) { 
       if (this.activeTool && this.activeTool.active) { 
        this.activeTool.move(event); 
       } 
      }.bind(this), false); 

      this.canvas.addEventListener('mouseup', function downHandler(event) { 
       if (this.activeTool) { 
        this.activeTool.end(event); 
       } 
      }.bind(this), false); 

      return this; 
     }, 
     'tool': function tool(toolName) { 
      if (toolName && this[toolName]) { 
       this.activeTool = this[toolName]; 
      } 
      return this.activeTool; 
     }, 
     'refresh': function refresh() { 

     }, 
     'scale': function scale(panel, canvas, panelInput) { 
      if (panel) { 
       this.panel.addEventListener('click', function(event) { 
        var panelValue = this.value; 
        var newWidth = canvas.width * panelValue; 
        var newHeight = canvas.height * panelValue; 
        panelInput.value = panelValue; 
        canvas.getContext('2d').scale(newWidth, newHeight); 
       }); 
      } else { 
       console.log("kein panel"); 
      } 
     }, 
    }; 

    var CanvasTool = function CanvasTool(canvas) { 
     if (!canvas) { 
      console.log(this.name + ' - no canvas'); 
     } 
     this.active = false; 
     this.canvas = canvas; 
     this.context = canvas.getContext('2d'); 
    }; 

    CanvasTool.prototype = { 
     'constructor': CanvasTool, 
     'coords': function coords(event) { 
      return { 
       'x': (event.layerX || event.offsetX), 
       'y': (event.layerY || event.offsetY) 
      }; 
     } 
    }; 

    var Tool_pencil = function Tool_pencil(canvas, color, width) { 
     CanvasTool.call(this, canvas); 
     this._color = color; 
     this._width = width; 
    }; 

    Tool_pencil.prototype = Object.create(CanvasTool.prototype, { 
     'constructor': { 
      'value': Tool_pencil 
     }, 
     'color': { 
      'value': function color(newColor) { 
       if (newColor) { 
        this._color = newColor; 
        return this; 
       } 
       return this._color; 
      } 
     }, 
     'end': { 
      'value': function end(event) { 
       this.move(event); 
       this.active = false; 
      } 
     }, 
     'move': { 
      'value': function move(event) { 
       var location = this.coords(event); 
       this.canvas.style.cursor = 'pointer'; 
       this.context.lineTo(location.x, location.y); 
       this.context.stroke(); 
       //if(myCanvasTools.panelInput && myCanvasTools.panelInput.value && myCanvasTools.panelInput.value != 0) { 
       myCanvasTools.scale(myCanvasTools.panel, myCanvasTools.canvas, myCanvasTools.panelInput); 
       this.context.stroke(); 
       //} 
      } 
     }, 
     'start': { 
      'value': function start(event) { 
       var location = this.coords(event); 
       this.context.beginPath(); 
       this.context.strokeStyle = this.color(); 
       this.context.lineWidth = this.width(); 
       this.context.moveTo(location.x, location.y); 
       this.active = true; 
      } 
     }, 
     'width': { 
      'value': function width(newWidth) { 
       if (newWidth) { 
        this._width = newWidth; 
        return this; 
       } 
       return this._width; 
      } 
     } 
    }); 

    return function canvasToolsFactory(settings) { 
     return new CanvasEditor(settings); 
    }; 
}()); 

// Canvas Modul Initialiseren 
var myCanvasTools = canvasTools({ 
    'canvas': document.querySelector('#imageView'), 
    'panel': document.querySelector('#canvasSliderPanel'), 
    'panelInput': document.querySelector('#canvasDepthValue'), 
    'color': 'black', 
    'width': 5 
}); 

myCanvasTools.init(); 

編輯 有些人建議我使用像fabric.js這樣的庫,但是有沒有辦法將這些結合起來呢?目前我不想重新編碼一切,但最糟糕的情況是我會。

回答

1

請找到下面提到的解決方案。這是你想要的代碼,這是爲演示目的而創建的。請根據您的要求進行修改。

$(document).ready(function(){ 
 

 
var canvas = new fabric.Canvas('paper', { 
 
     isDrawingMode: false, 
 
     height: 200, 
 
     width: 200, 
 
     top: 0, 
 
     left: 0 
 
    }); 
 
    var isDown; 
 

 
    canvas.isDrawingMode = true; 
 
    var drawingColorEl = $('#drawing-color'); 
 
    var drawingLineWidthEl = $('#drawing-line-width'); 
 

 
    canvas.freeDrawingBrush.color = drawingColorEl.val(); 
 
    canvas.freeDrawingBrush.width = drawingLineWidthEl.val(); 
 

 
    drawingColorEl.change(function() { 
 
     isDown = true; 
 
     canvas.isDrawingMode = true; 
 
     canvas.freeDrawingBrush.color = this.value; 
 
    }); 
 

 
    drawingLineWidthEl.change(function() { 
 
     isDown = true; 
 
     canvas.isDrawingMode = true; 
 
     canvas.freeDrawingBrush.width = drawingLineWidthEl.val(); 
 
    }); 
 

 

 

 
    $('#selectMode').click(function() { 
 
     var objs = []; 
 
     objs = canvas.getObjects(); 
 

 
     var alltogetherObj = new fabric.Group(objs, { 
 
     }); 
 
     canvas._activeObject = null; 
 
     if (objs.length != 0) { 
 
      canvas.setActiveGroup(alltogetherObj.setCoords()).renderAll(); 
 
     } 
 
     isDown = false; 
 
     canvas.isDrawingMode = false; 
 

 
    }); 
 

 
    $('#drawMode').click(function() { 
 
     isDown = true; 
 
     canvas.isDrawingMode = true; 
 
     canvas.discardActiveGroup(); 
 
     canvas.discardActiveObject(); 
 
     canvas.renderAll(); 
 
    }); 
 

 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script> 
 
<script src="//cdnjs.cloudflare.com/ajax/libs/fabric.js/1.7.12/fabric.min.js"></script> 
 

 
<select id="drawing-color"> 
 
    <option value="#ff0000">Red</option> 
 
    <option value="#000000">Black</option> 
 
</select> 
 

 
<select id="drawing-line-width"> 
 
    <option value="1">1</option> 
 
    <option value="2">2</option> 
 
    <option value="3">3</option> 
 
    <option value="4">4</option> 
 
    <option value="5" selected="selected">5</option> 
 
    <option value="6">6</option> 
 
    <option value="7">7</option> 
 
    <option value="8">8</option> 
 
    <option value="9">9</option> 
 
    <option value="10">10</option> 
 
</select> 
 
<input type="button" id="selectMode" value="Select Mode" /> 
 
<input type="button" id="drawMode" value="Draw Mode"/> 
 
<canvas id="paper" style="border:1px solid #ccc"></canvas>

讓我知道,如果它不爲你工作。

+0

你有沒有人可以繪製和選擇區域+尺寸函數的例子? – utdev

+0

所以你的意思是說繪製矩形的創建函數..?像這樣你問 –

+0

我現在的腳本讓我們用鼠標畫線,畫圈等,我想選擇我用鼠標畫的畫。你有這樣的例子嗎? – utdev

0

這不是簡單的任務,並從我的意見,你應該使用類似pixie.js庫,請看看這個例子 -

http://pixijs.github.io/examples/#/demos/dragging.js

但是,如果你真的想實現它憑藉自我,這將是一個真正的挑戰。我可以建議在這裏檢查我的代碼 - http://kill-them.com/assets/js/applications/MapEditor.js?v=7

要檢查它是如何工作的,你可以去http://kill-them.com/,按short session然後去map editor在頂部的菜單。在那裏你可以看到地圖編輯器,你可以在那裏放置牆。那裏會有控制,如調整大小和旋轉。但是,真的,如果我將再次從頭開始同一個項目,我將使用庫。