资讯专栏INFORMATION COLUMN

Fabric.js 的简单介绍和使用 (二)

wangdai / 313人阅读

摘要:之前我们学习了基础用法现在我们开始一些好玩的我们先回顾设置一下正方形角度方法这是没有动画的都有方法那么正方形会从到有一个动画过度从左到右进行变动逆时针转度当然还支持这些方法图片可以使用效果一次可以使用多个效果当然你也可以自己定义支持颜色

之前我们学习了基础用法 现在我们开始一些好玩的

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
a multiline
text
aligned 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
a multiline
text
with
custom lineheight
&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");
});

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/79153.html

相关文章

  • Fabric.js 简单介绍使用

    摘要:简介是一个可以简化程序编写的库。为提供所缺少的对象模型交互和一整套其他不可或缺的工具。基于协议开源,在上有许多人贡献代码。在移动,线,曲线,或弧等命令的帮助下,路径可以形成令人难以置信的复杂形状。同组的路径路径组的帮助,开放更多的可能性。 简介 Fabric.js是一个可以简化canvas程序编写的库。 Fabric.js为canvas提供所缺少的对象模型, svg parser, 交...

    piapia 评论0 收藏0
  • Fabric.js 简单介绍使用 (三)

    摘要:之前我们学习了基础和高级特性现在介绍更神奇的东西话说这个功能我最喜欢组成群组可以统一修改其中所有组件属性如何定义现在我们就可以对其中的对象集修改中的元素相对于定位但是由于要确保之前得到却切位置所以要异步可以动态添加添加并修改当然你可以使用 之前我们学习了基础和高级特性 现在介绍更神奇的东西 Groups 话说这个功能我最喜欢 组成群组可以统一修改其中所有组件属性 如何定义 var...

    xi4oh4o 评论0 收藏0
  • 我从 fabric.js 中学到了什么

    摘要:前言熟悉的朋友想必都使用或者听说过,算是一个元老级的库了,从第一个版本发布到现在,已经有年时间了。中缓存是默认开启的,同时也可以设置为禁用。处理屏屏幕模糊的问题,直接给出处理方法,就不展开说了。 前言 熟悉 canvas 的朋友想必都使用或者听说过 Fabric.js,Fabric 算是一个元老级的 canvas 库了,从第一个版本发布到现在,已经有 8 年时间了。我近一年时间也在项目...

    oogh 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<