前端前端
fabric
算法
jsBlock
styleBlock
svgBlock
工具其他
Vue、React相关
webgl
GitHub
fabric
算法
jsBlock
styleBlock
svgBlock
工具其他
Vue、React相关
webgl
GitHub
  • 元素操作

    • 元素分组
    • 元素定位
    • 元素层级
    • 元素禁止事件
    • 元素选中样式
  • 变换

    • 变换
  • 图片

    • 图片
    • 背景图
  • 基础图形

    • 三角形
    • 圆形
    • 多边形
    • 折线
    • 椭圆
    • 直线
    • 矩形
    • 路径
  • 序列化和反序列化

    • 反序列化
    • 输出JSON
    • 输出png
    • 输出svg
  • 文本

    • 可编辑文本
    • 多行文本
    • 文本
    • 文本省略
    • 文本边框
  • 源码

    • fabric.Canvas
    • fabric.Circle
    • fabric类的继承体系
  • 画布操作

    • 画布拖动和缩放
    • 画布框选样式
  • 示例

    • fabric-echarts-demo
    • 拓扑图一
    • 拓扑图二

fabric.Canvas

fabric是个对象, 它上面挂载了很多方法

-- fabric.StaticCanvas // 创建

-- fabric.util.xx // 工具类

-- fabric.CommonMethods.xx // 公共方法

-- fabric.Circle

fabric.StaticCanvas

定义

fabric.StaticCanvas, 调用 fabric.util.createClass,返回function,该function的原型上会添加fabric.CommonMethods.xx, fabric.Collection.xx 等等

fabric.StaticCanvas = fabric.util.createClass(fabric.CommonMethods, fabric.Collection, {
    initialize: function ...,
    backgroundColor: '',
    ...
}
fabric.CommonMethods = {
    _setOptions: function ...,
    ...
}
fabric.Collection = {
    _objects: [],
    add: function ...
    ...
}

// 省略了些
fabric.util.createClass = function() {
    function klass() {
      this.initialize.apply(this, arguments);
    }
    const properties = slice.call(arguments, 0);
    for (var i = 0, length = properties.length; i < length; i++) {
      addMethods(klass, properties[i], parent);
    }
    return klass
}
fabric.StaticCanvas = function() {
    // 生成实例会执行
    this.initialize.apply(this, arguments);
}
fabric.StaticCanvas.prototype.initialize = function() {...}
fabric.StaticCanvas.prototype._setOptions = function() {...}
fabric.StaticCanvas.prototype.add = function() {...}
fabric.StaticCanvas.prototype._objects = []

生成实例

new fabric.StaticCanvas(canvasEl, options) 生成实例,执行 initialize

initialize: function(el, options) {
    options || (options = { });
    this.renderAndResetBound = this.renderAndReset.bind(this);
    this.requestRenderAllBound = this.requestRenderAll.bind(this);
    this._initStatic(el, options); // 这个只生成loverCanvas,并设置背景
},

this.renderAndReset 重置canvas

renderCanvas: function(ctx, objects) {
    var v = this.viewportTransform, path = this.clipPath;
    this.cancelRequestedRender(); // 消除动画
    // 计算canvas边界, 可以调用canvas.calcViewportBoundaries() 返回
    // this.vptCoords = {
    //    tl: min,
    //    tr: new Point(max.x, min.y),
    //    bl: new Point(min.x, max.y),
    //    br: max,
    //  };
    this.calcViewportBoundaries(); 
    this.clearContext(ctx);
    ...
}

new fabric.Canvas(canvasEl, options) 生成实例,执行 initialize, 和 StaticCanvas一样,initialize方法多了

initialize: function(el, options) {
    options || (options = { });
    this.renderAndResetBound = this.renderAndReset.bind(this);
    this.requestRenderAllBound = this.requestRenderAll.bind(this);
    this._initStatic(el, options);

    this._initInteractive(); // 多了这个
    this._createCacheCanvas(); // 多了这个, 缓存canvas
},

this._initInteractive()

  • 添加upperCanvas, 和lowerCanvas一起被canvas-container包裹
  • 初始化监听事件
  • 笔刷
    this.wrapperEl = <div class="canvas-container'>
        <canvas class="upper-canvas"></canvas>
        <canvas class="lower-canvas"></canvas>
    </div>
    this.contextContainer = lowerCanvasEl.getContext('2d');
    this.contextTop =  upperCanvasEl.getContext('2d');

this._createCacheCanvas(); 缓存canvas

this.cacheCanvasEl = this._createCanvasElement();
this.contextCache = this.cacheCanvasEl.getContext('2d');
Edit this page
最近更新:: 2023/12/8 15:40
Contributors: wu.hui
Next
fabric.Circle