javascript Fabric.js - 更改矩形填充
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12356493/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Fabric.js - Change Rectangle Fill
提问by Kalvin Klien
Hey guys I'm working on a application that uses Fabric.js.
大家好,我正在开发一个使用 Fabric.js 的应用程序。
Fabric.js API: http://fabricjs.com/docs/symbols/fabric.Object.html
Fabric.js API:http://fabricjs.com/docs/symbols/fabric.Object.html
I need to be able to change the fillof the rectanglethat I put up as background.
我需要能够更改作为背景放置的矩形的填充。
I use canvas.getActiveObject() to change the fill of the rectangle. Unfortunately I cant seem to find a method that will change the Fill of the rectangle.
我使用 canvas.getActiveObject() 来改变矩形的填充。不幸的是,我似乎无法找到一种可以更改矩形填充的方法。
Here is my code:
这是我的代码:
<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>
Any ideas on how to change it? Thanks in advance!
关于如何改变它的任何想法?提前致谢!
回答by Kalvin Klien
Fixed the problem by using basic set method of fabric:
使用fabric的基本设置方法修复了问题:
backgroundColor.onchange = function() {
canvas.getActiveObject().set("fill", this.value);
canvas.renderAll();
};
回答by satyawan
tri this the easy and simple code by fabrics documentation
通过fabric 文档来验证这个简单而简单的代码
var activeObject = canvas.getActiveObject();
activeObject.fill = 'your color code value';
canvas.renderAll();
回答by DarkDaryl
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"