克隆一个画布到另一个有麻烦

时间:2015-09-15 13:10:03

标签: javascript jquery html5 canvas fabricjs

我已经检查了将现有画布克隆到另一个画布的所有答案。但我无法完成它。

请检查我目前的进度。 http://jsfiddle.net/37n8rtdf/5/

第一个画布将被剪裁到您最初看到的那个方块,该画布的内容将被添加到另一个画布中。但我不知道它总是把TYPE_MISMATCH_ERR: DOM Exception 171扔进chrome。我正在使用fabricjs剪辑内容。

赞赏一点帮助。

由于

这是我的脚本代码:

HTML

        <textarea id="line_1"></textarea>
        <input type="button" id="render" value="Apply" />

        <input type="button" id="preview" value="preview" />


        <canvas id="c" width="500" height="500" style="border:1px solid #ccc"></canvas>


        <canvas id="c_new" width="500" height="500" style="border:1px solid red; margin: 30px;"></canvas>

            `

的Javascript

            var canvas = this.__canvas = new fabric.Canvas('c');
            var canvas_new = this.__canvas = new fabric.Canvas('c_new');
            var product_image = 'http://www.jail.se/hardware/digital_camera/canon/ixus_800is-powershot_sd700/images/sample_photos/sample3.jpg';
            //var ctx = canvas.getContext("2d");
            var polygon;
            $(document).ready(function(){

            fabric.Object.prototype.transparentCorners = false;


            //canvas.setDimensions({width:w,height:h});
            var center = canvas.getCenter();
            canvas.setBackgroundImage(product_image,
                canvas.renderAll.bind(canvas), {
                    scaleX:1,
                    scaleY:1,
                    top: center.top,
                    left: center.left,
                    originX: 'center',
                    originY: 'center',
                    backgroundImageOpacity: 0,
                    backgroundImageStretch: false
            });
            canvas_new.setBackgroundImage(product_image,
                canvas_new.renderAll.bind(canvas_new), {
                    scaleX:1,
                    scaleY:1,
                    top: center.top,
                    left: center.left,
                    originX: 'center',
                    originY: 'center'
            });




                polygon = new fabric.Polygon([
                    {x: 0, y: 0},
                    {x: 220, y: 0},
                    {x: 220, y: 180},
                    {x: 0, y: 180} ], {
                    left: 140,
                    top: 150,
                    angle: 0,
                    fill: 'transparent',
                    stroke: '#000', strokeWidth: 1,
                    lockMovementX: true,
                    lockMovementY: true,
                    lockScalingX: true,
                    lockScalingY: true,
                    lockRotation: true,
                    hasControls: false,
                    hasBorders: false,
                    hoverCursor: 'default',
                    overflow: 'hidden'
                });
                canvas.add(polygon);
         $('#render').click(function(){ return render(); });

                $('#preview').click(function(){ return rasterize(); });


        });

        function render()
        {
            var text_val = $('#line_1').val();

            var comicSansText = new fabric.Text(text_val, {
              fontWeight: 'normal'
            });
            canvas.add(comicSansText.set({ left: 200, top: 150, angle: 0 }));
        }




          function rasterize()
          {
              var shape = canvas.item(0);
              polygon.strokeWidth=0;
              canvas.renderAll();
            //canvas.remove(shape);
            canvas.clipTo = function(ctx) {
                shape.render(ctx);
            };

            var ctx2    = canvas_new.getContext('2d');
            ctx2.drawImage(canvas, 0, 0);
          }

`

1 个答案:

答案 0 :(得分:0)

@WinterMute是对的,您正在尝试绘制Fabric对象而不是canvas元素。

您的布料对象似乎嵌入了两个画布(lowerCanvasElupperCanvasEl)。 因此,您可以稍微修改一下代码,使其看起来像:

function rasterize() {
  var shape = canvas.item(0);
  polygon.strokeWidth = 0;
  canvas.renderAll();

  canvas.clipTo = function(ctx) {
    shape.render(ctx);
  };
  var ctx2 = canvas_new.getContext('2d');
  ctx2.drawImage(canvas.lowerCanvasEl, 0, 0);
  ctx2.drawImage(canvas.upperCanvasEl, 0, 0);
}

Updated fiddle