2012-09-19 40 views
3

我正在尋找關於如何替換fabric.js調整大小和旋轉矩形與自定義圖像的例子....有任何人這樣做?替換fabric.js調整大小並使用自定義圖像旋轉控件?

謝謝。

+0

這是不是現在很容易,但通過在每個頂部更改源代碼或顯示圖像控制與此類似演示 - HTTP: //fabricjs.com/interaction-with-objects-outside-canvas/ – kangax

+1

這個問題是從2012年開始的,如果你通過谷歌來到這裏,你可能也想看看這個更近的回答:http://stackoverflow.com/a/24533063/29182 – Ziggy

回答

10

您可以通過重寫原型是這樣做的:

var topLeftImage = new Image(); 
topLeftImage.src = 'images/tl.png'; 

var topRightImage = new Image(); 
topRightImage.src = 'images/tr.png'; 

var bottomRightImage = new Image(); 
bottomRightImage.src = 'images/br.png'; 

//Warning I modified some other things here as well, please copy this from the sources and modify it then 
fabric.Object.prototype.drawCorners = function(ctx) { 
    if (!this.hasControls) return; 

    var size = this.cornersize, 
     size2 = size/2, 
     strokeWidth2 = this.strokeWidth/2, 
     left = -(this.width/2), 
     top = -(this.height/2), 
     _left, 
     _top, 
     sizeX = size/this.scaleX, 
     sizeY = size/this.scaleY, 
     paddingX = this.padding/this.scaleX, 
     paddingY = this.padding/this.scaleY, 
     scaleOffsetY = size2/this.scaleY, 
     scaleOffsetX = size2/this.scaleX, 
     scaleOffsetSizeX = (size2 - size)/this.scaleX, 
     scaleOffsetSizeY = (size2 - size)/this.scaleY, 
     height = this.height, 
     width = this.width; 

    ctx.save(); 

    ctx.lineWidth = 1/Math.max(this.scaleX, this.scaleY); 

    ctx.globalAlpha = 1; //this.isMoving ? this.borderOpacityWhenMoving : 1; 
    ctx.strokeStyle = ctx.fillStyle = '#333333'; 

    // top-left 
    _left = left - scaleOffsetX - strokeWidth2 - paddingX; 
    _top = top - scaleOffsetY - strokeWidth2 - paddingY; 

    ctx.clearRect(_left, _top, sizeX, sizeY); 
    ctx.drawImage(topLeftImage, _left, _top, sizeX, sizeY); 

    // top-right 
    _left = left + width - scaleOffsetX + strokeWidth2 + paddingX; 
    _top = top - scaleOffsetY - strokeWidth2 - paddingY; 

    ctx.clearRect(_left, _top, sizeX, sizeY); 
    ctx.drawImage(topRightImage, _left, _top, sizeX, sizeY); 

    // bottom-left 
    _left = left - scaleOffsetX - strokeWidth2 - paddingX; 
    _top = top + height + scaleOffsetSizeY + strokeWidth2 + paddingY; 

    ctx.clearRect(_left, _top, sizeX, sizeY); 
    ctx.strokeRect(_left, _top, sizeX, sizeY); 

    // bottom-right 
    _left = left + width + scaleOffsetSizeX + strokeWidth2 + paddingX; 
    _top = top + height + scaleOffsetSizeY + strokeWidth2 + paddingY; 

    ctx.clearRect(_left, _top, sizeX, sizeY); 
    ctx.drawImage(bottomRightImage, _left, _top, sizeX, sizeY); 

    ctx.restore(); 

    return this; 
}; 
+2

您好@Alexander Kludt您可以請更多地解釋您的代碼,在哪裏添加此代碼或在織物庫中重寫它。請指教 –