【问题标题】:Fabric.js: How to serialize clipTo objects since ToJSON does not work for it?Fabric.js:如何序列化 clipTo 对象,因为 ToJSON 不适用于它?
【发布时间】:2016-12-08 13:59:14
【问题描述】:

任何关于显示 ClipTo 序列化的 jsfiddle 示例的指导将不胜感激?当前 ToJSON 函数在尝试序列化剪辑对象时不起作用。请参阅代码底部的 ToJSON 实现。

JSFiddle 链接: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;

//convert to json
var serialized=JSON.stringify(canvas); 

canvas.clear();
canvas.loadFromDatalessJSON(serialized);
alert(serialized);

【问题讨论】:

  • @kangax:会感谢您的意见吗?谢谢
  • 你能弄明白吗?我也陷入了同样的境地。谢谢

标签: fabricjs


【解决方案1】:

fabricjs clipTo 默认情况下应该包含在画布的 json 表示中。

因此,如果您使用toJSON,您将在包含clipTo 函数的画布的json 表示中找到clipTo 字段。

这是demo

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-28
    • 2013-10-22
    • 2013-08-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-29
    • 2019-06-03
    相关资源
    最近更新 更多