使用fabric.js将画布形状绘制为椭圆形布局

问题描述 投票:1回答:1

在更改事件我试图绘制画布为椭圆形状,我没有获得画布的适当属性以椭圆形格式绘制此画布。我在我的代码中设置了以下属性请参阅以下代码。我尝试使用css和我我正在变得完美,但是当我尝试将画布转换为png图像时画布形状不会在图像中转换,这就是为什么,我想以这种方式。

$("#decalshap").change(function() {
         alert("oval");
         var shap = $(this).val();
     if(shap=="oval")
        {
    canvas.set({
    height:314,
    width:500,       
    borderColor: 'red',
    borderRadius: 314.5/157.25,
    border:2,
    });  
 }

答案将不胜感激。

javascript css html5 canvas fabricjs
1个回答
1
投票

为此,你必须使用clipTo属性,使用此代码在canvas //脚本中绘制一个椭圆

 var w;
 var h;
 var ctx = canvas.getContext('2d');
 w=canvas.width / 4;
 h=canvas.height / 2.4;
 canvas.clipTo = function(ctx) {
 ctx.save();
 ctx.scale(2, 1.2);
 ctx.arc(w, h, 90, 0, 2 * Math.PI, true);
 ctx.stroke();
 ctx.restore();
 //canvas.renderAll();
}

这是小提琴DEMO

© www.soinside.com 2019 - 2024. All rights reserved.