之前我们进修了基本用法 如今我们最先一些好玩的
Animation
我们先回忆设置一下正方形角度要领
rect.set('angle', 45);
这是没有动画的
Fabric object都有animate要领
rect.animate('angle', 45, {
onChange: canvas.renderAll.bind(canvas)
});
那末正方形会从0到45有一个动画过分
从左到右举行更改
rect.animate('left', '+=100', { onChange: canvas.renderAll.bind(canvas) });
逆时针转5度
rect.animate('angle', '-=5', { onChange: canvas.renderAll.bind(canvas) });
固然animate还支撑这些要领
from: Allows to specify starting value of animatable property (if we don’t want 2. current value to be used).
duration: Defaults to 500 (ms). Can be used to change duration of an animation.
onComplete: Callback that’s invoked at the end of the animation.
easing: Easing function.
rect.animate('left', '+=100', {
onChange: canvas.renderAll.bind(canvas),
duration: 3000,
easing: fabric.util.ease.easeOutBounce
});
Image filters
图片能够运用filter结果
fabric.Image.fromURL('pug.jpg', function(img) {
// add filter
img.filters.push(new fabric.Image.filters.Grayscale());
// apply filters and re-render canvas when done
img.applyFilters(canvas.renderAll.bind(canvas));
// add image onto canvas
canvas.add(img);
});
filter一次能够运用多个结果
固然 你也能够本身定义filter
fabric.Image.filters.Redify = fabric.util.createClass({
type: 'Redify',
applyTo: function(canvasEl) {
var context = canvasEl.getContext('2d'),
imageData = context.getImageData(0, 0, canvasEl.width, canvasEl.height),
data = imageData.data;
for (var i = 0, len = data.length; i < len; i += 4) {
data[i + 1] = 0;
data[i + 2] = 0;
}
context.putImageData(imageData, 0, 0);
}
});
fabric.Image.filters.Redify.fromObject = function(object) {
return new fabric.Image.filters.Redify(object);
};
Colors
fabric 支撑 hex rgb rgba色彩
new fabric.Color('#f55');
new fabric.Color('#123123');
new fabric.Color('356735');
new fabric.Color('rgb(100,0,100)');
new fabric.Color('rgba(10, 20, 30, 0.5)');
而且支撑互相转换
new fabric.Color('#f55').toRgb(); // "rgb(255,85,85)"
new fabric.Color('rgb(100,100,100)').toHex(); // "646464"
new fabric.Color('fff').toHex(); // "FFFFFF"
两种色彩能够叠加 而且你能够运用一些特定结果
var redish = new fabric.Color('#f55');
var greenish = new fabric.Color('#5f5');
redish.overlayWith(greenish).toHex(); // "AAAA55"
redish.toGrayscale().toHex(); // "A1A1A1"
Gradients
能够运用渐变色
var circle = new fabric.Circle({
left: 100,
top: 100,
radius: 50
});
circle.setGradient('fill', {
x1: 0,
y1: -circle.height / 2,
x2: 0,
y2: circle.height / 2,
colorStops: {
0: '#000',
1: '#fff'
}
});
起首肯定两个点 在其间隔中以百分比定位色彩
circle.setGradient('fill', {
x1: -circle.width / 2,
y1: 0,
x2: circle.width / 2,
y2: 0,
colorStops: {
0: "red",
0.2: "orange",
0.4: "yellow",
0.6: "green",
0.8: "blue",
1: "purple"
}
});
Text
Multiline support. Native text methods unfortunately simply ignore new lines.
Text alignment. Left, center, right. Useful when working with multiline text.
Text background. Background also respects text alignment.
Text decoration. Underline, overline, strike-through.
Line height. Useful when working with multiline text.
怎样增加笔墨
var text = new fabric.Text('hello world', { left: 100, top: 100 });
canvas.add(text);
fontFamily
var comicSansText = new fabric.Text("I'm in Comic Sans", {
fontFamily: 'Comic Sans'
});
fontSize
var text40 = new fabric.Text("I'm at fontSize 40", {
fontSize: 40
});
var text20 = new fabric.Text("I'm at fontSize 20", {
fontSize: 20
});
fontWeight
var normalText = new fabric.Text("I'm a normal text", {
fontWeight: 'normal'
});
var boldText = new fabric.Text("I'm at bold text", {
fontWeight: 'bold'
});
textDecoration
var underlineText = new fabric.Text("I'm an underlined text", {
textDecoration: 'underline'
});
var strokeThroughText = new fabric.Text("I'm a stroke-through text", {
textDecoration: 'line-through'
});
var overlineText = new fabric.Text("I'm an overline text", {
textDecoration: 'overline'
});
shadow
var shadowText1 = new fabric.Text("I'm a text with shadow", {
shadow: 'rgba(0,0,0,0.3) 5px 5px 5px'
});
var shadowText2 = new fabric.Text("And another shadow", {
shadow: 'rgba(0,0,0,0.2) 0 0 5px'
});
var shadowText3 = new fabric.Text("Lorem ipsum dolor sit", {
shadow: 'green -5px -5px 3px'
});
fontStyle
var italicText = new fabric.Text("A very fancy italic text", {
fontStyle: 'italic',
fontFamily: 'Delicious'
});
var anotherItalicText = new fabric.Text("another italic text", {
fontStyle: 'italic',
fontFamily: 'Hoefler Text'
});
stroke & strokeWidth
var textWithStroke = new fabric.Text("Text with a stroke", {
stroke: '#ff1318',
strokeWidth: 1
});
var loremIpsumDolor = new fabric.Text("Lorem ipsum dolor", {
fontFamily: 'Impact',
stroke: '#c3bfbf',
strokeWidth: 3
});
textAlign
var text = 'this is\na multiline\ntext\naligned right!';
var alignedRightText = new fabric.Text(text, {
textAlign: 'right'
});
lineHeight
var lineHeight3 = new fabric.Text('Lorem ipsum ...', {
lineHeight: 3
});
var lineHeight1 = new fabric.Text('Lorem ipsum ...', {
lineHeight: 1
});
textBackgroundColor
var text = 'this is\na multiline\ntext\nwith\ncustom lineheight\n&background';
var textWithBackground = new fabric.Text(text, {
textBackgroundColor: 'rgb(0,200,0)'
})
Events
怎样能够没有事宜呢
事宜以on off运用 canvas 能够捕获事宜
mouseevent
“mouse:down”, “mouse:move”, and “mouse:up”.
renderevent
“after:render”.
selectionevent
“before:selection:cleared”, “selection:created”, “selection:cleared”.
objectevent
object ones: “object:modified”, “object:selected”, “object:moving”, “object:scaling”, “object:rotating”, “object:added”, and “object:removed”
var canvas = new fabric.Canvas('...');
canvas.on('mouse:down', function(options) {
console.log(options.e.clientX, options.e.clientY);
});
一样这些事宜也能够用任何fabric对象监听
var rect = new fabric.Rect({ width: 100, height: 50, fill: 'green' });
rect.on('selected', function() {
console.log('selected a rectangle');
});
var circle = new fabric.Circle({ radius: 75, fill: 'blue' });
circle.on('selected', function() {
console.log('selected a circle');
});