簡體   English   中英

如何將畫布放入畫布中?

[英]How to put canvas within canvas?

我只想知道在單個頁面中使用多個畫布的最佳方法是什么。 這些畫布可以相互重疊。

我試圖以其他形式搜索此問題,但找不到任何有用的材料。 這是我們實際上想要做的(下圖)。 有5個畫布,我們希望它們全部功能齊全。 我們可以在選定的畫布上添加圖像,文本和繪制其他內容。

我們目前正在使用fabricjs。 在此處輸入圖片說明

如果那不可能,那么實現這種目標的最佳解決方案是什么?

提前致謝!

只需使用CSS即可。

<div class="wrapper">
 <canvas id="background_layer" class="canvas-layer" width="100" height="100"></canvas>
 <canvas id="other_layer" class="canvas-layer" width="100" height="100"></canvas>
</div>

<style>
    .wrapper { position: relative }
    .canvas-layer {
        position: absolute; left: 0; top: 0; 
    }
</style>

我不確定您要實現的目標,但是您可以參考此小提琴http://jsfiddle.net/PromInc/ZxYCP/

var img01URL = 'https://www.google.com/images/srpr/logo4w.png';
var img02URL = 'http://fabricjs.com/lib/pug.jpg';

var canvas = new fabric.Canvas('c');

// Note the use of the `originX` and `originY` properties, which we set
// to 'left' and 'top', respectively. This makes the math in the `clipTo`
// functions a little bit more straight-forward.
var clipRect1 = new fabric.Rect({
    originX: 'left',
    originY: 'top',
    left: 180,
    top: 10,
    width: 200,
    height: 200,
    fill: '#DDD', /* use transparent for no fill */
    strokeWidth: 0,
    selectable: false
});
// We give these `Rect` objects a name property so the `clipTo` functions can
// find the one by which they want to be clipped.
clipRect1.set({
    clipFor: 'pug'
});
canvas.add(clipRect1);

var clipRect2 = new fabric.Rect({
    originX: 'left',
    originY: 'top',
    left: 10,
    top: 10,
    width: 150,
    height: 150,
    fill: '#DDD', /* use transparent for no fill */
    strokeWidth: 0,
    selectable: false
});
// We give these `Rect` objects a name property so the `clipTo` functions can
// find the one by which they want to be clipped.
clipRect2.set({
    clipFor: 'logo'
});
canvas.add(clipRect2);

function findByClipName(name) {
    return _(canvas.getObjects()).where({
            clipFor: name
        }).first()
}

// Since the `angle` property of the Image object is stored 
// in degrees, we'll use this to convert it to radians.
function degToRad(degrees) {
    return degrees * (Math.PI / 180);
}

var clipByName = function (ctx) {
    this.setCoords();
    var clipRect = findByClipName(this.clipName);
    var scaleXTo1 = (1 / this.scaleX);
    var scaleYTo1 = (1 / this.scaleY);
    ctx.save();

    var ctxLeft = -( this.width / 2 ) + clipRect.strokeWidth;
        var ctxTop = -( this.height / 2 ) + clipRect.strokeWidth;
        var ctxWidth = clipRect.width - clipRect.strokeWidth;
        var ctxHeight = clipRect.height - clipRect.strokeWidth;

    ctx.translate( ctxLeft, ctxTop );

    ctx.rotate(degToRad(this.angle * -1));
    ctx.scale(scaleXTo1, scaleYTo1);
    ctx.beginPath();
    ctx.rect(
        clipRect.left - this.oCoords.tl.x,
        clipRect.top - this.oCoords.tl.y,
        clipRect.width,
        clipRect.height
    );
    ctx.closePath();
    ctx.restore();
}

var pugImg = new Image();
pugImg.onload = function (img) {    
    var pug = new fabric.Image(pugImg, {
        angle: 45,
        width: 500,
        height: 500,
        left: 230,
        top: 50,
        scaleX: 0.3,
        scaleY: 0.3,
        clipName: 'pug',
        clipTo: function(ctx) { 
            return _.bind(clipByName, pug)(ctx) 
        }
    });
    canvas.add(pug);
};
pugImg.src = img02URL;

var logoImg = new Image();
logoImg.onload = function (img) {    
    var logo = new fabric.Image(logoImg, {
        angle: 0,
        width: 550,
        height: 190,
        left: 50,
        top: 50,
        scaleX: 0.25,
        scaleY: 0.25,
        clipName: 'logo',
        clipTo: function(ctx) { 
            return _.bind(clipByName, logo)(ctx) 
        }
    });
    canvas.add(logo);
};
logoImg.src = img01URL;

我希望這會有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM