Fabric.js-更改矩形填充
Fabric.js - Change Rectangle Fill
我正在开发一个使用Fabric.js.的应用程序
我需要能够更改我作为背景设置的矩形的fill。
我使用canvas.getActiveObject()
来更改矩形的填充。不幸的是,我似乎找不到一个可以改变矩形填充的方法。
这是我的代码:
<html>
<head>
<title>Text Rendering Example</title>
<script src="fabric.js"></script>
<script src="canvas2image.js"></script>
<script src="base64.js"></script>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"
</head>
<body>
<canvas id="canvas" width="1000" height="600" style="border:1px solid #000000"></canvas>
<form name = "boxForm" onsubmit="addBox()">
Width: <input type="text" name="firstname" /><br />
Text: <input id="text-control" type="text" name="textString" />
Text Color: <input id="text-color" type="text" value = "#FF0000" name="textColor" />
Background Color: <input id="background-color" type="text" value = "#333333" name="backgroundColor" />
</form>
<button onclick="addBox()">Background</button>
<button onclick="addText()">Add Text</button>
<!--button onclick="updateControls()">Edit Text</button-->
<button onclick="saveImage()">File</button>
<script type="text/javascript" >
var canvas = new fabric.Canvas('canvas');
var $ = function(id){return document.getElementById(id)};
var textArray = new Array();
var textControl = $('text-control');
var textColor = $('text-color');
var backgroundColor = $('background-color');
function addBox()
{
var rect = new fabric.Rect({
width: 1000,
height: 600,
top: 300,
left: 500,
fill: 'rgba(51,51,51,1)',
draggable: false
});
rect.lockMovementX = true;
rect.lockMovementY = true;
rect.lockUniScaling = true;
rect.lockRotation = true;
canvas.add(rect);
}
function addText()
{
var content = document.boxForm.textString.value;
var color = document.boxForm.textColor.value;
text = new fabric.Text(content, {
left: 100,
top: 100,
fill: color
});
text.lockUniScaling = true;
text.lockRotation = true;
//textArrayAdd(text);
canvas.add(text);
}
textControl.onchange = function() {
canvas.getActiveObject().setText(this.value);
canvas.renderAll();
};
textColor.onchange = function() {
canvas.getActiveObject().setColor(this.value);
canvas.renderAll();
};
/*----This is where I change the color of the Rectangle-----*/
backgroundColor.onchange = function() {
canvas.getActiveObject().fillRect( 20, 100, 100, 50 );
canvas.renderAll();
};
/*----This is where I change the color of the Rectangle-----*/
/*function textArrayAdd(obj)
{
textArray.push(obj);
}*/
function updateControls() {
textControl.value = canvas.getActiveObject().getText();
}
canvas.on({
'object:selected': updateControls,
});
/*var strDataURI = canvas.toDataURL('image/png');
function saveImage()
{
Canvas2Image.saveAsPNG(canvas);
}*/
</script>
</body>
</html>
有什么改变的想法吗?提前感谢!
使用fabric的基本集合方法修复了问题:
backgroundColor.onchange = function() {
canvas.getActiveObject().set("fill", this.value);
canvas.renderAll();
};
tri这是通过结构文档编写的简单易用的代码
var activeObject = canvas.getActiveObject();
activeObject.fill = 'your color code value';
canvas.renderAll();
function addBox()
{
var rect = new fabric.Rect({
width: 1000,
height: 600,
top: 300,
left: 500,
fill: ' ',
draggable: false
});
rect.lockMovementX = true;
rect.lockMovementY = true;
rect.lockUniScaling = true;
rect.lockRotation = true;
canvas.add(rect);
}
make your fill null and if you want to have a border
use strokeWidth: 3, stroke: "color"
相关文章:
- spine.js在启动时填充模型实例
- Fabric.js-更改矩形填充
- 在EXT窗体面板中填充EXT JS存储
- 使用kendo js填充网格
- 如何在js中实现树结构类型的表单动态填充
- 如何在d3.js中填充svg圆圈内的图像
- 在Angular JS中,预先填充ng中的下拉值repeat
- 在Sails.js中通过关系填充多对多
- 在Angular JS中填充对象以进行插入
- 在 Node.js 中填充来自猫鼬的对象
- JS使用不同的填充
- 意思是.JS中间件中的猫鼬填充不起作用
- 设置图像以使用纸张.js填充画布
- 面积图的条件填充 D3.js
- 如何以类似于vivus.js的样式设置SVG填充和路径的动画
- 在刀片模板中填充js数组
- 用PHP填充JS代码段
- PHP:使用opendir填充JS幻灯片
- 在特定索引中填充JS数组
- 如何在我的谷歌浏览器扩展程序中将信息从后台传递.js到自动填充.js