简体   繁体   English

fabric.js,在context上创建一些形状

[英]fabric.js, create some shapes on contextTop

This is a question about fabric.js. 这是关于fabric.js的问题。 I have been trying to create some shapes on the contextTop. 我一直在尝试在contextTop上创建一些形状。 When you create some shapes on contextTop(not using add ), how to merge them with the fabric.js canvas? 当您在contextTop上创建一些形状(不使用add )时,如何将它们与fabric.js画布合并?

let canvas = new fabric.Canvas('C');
canvas.contextTop.StrokeRect(12,12,30,30);

Then, I want to make the Rect appear on the canvas. 然后,我要使Rect出现在画布上。 What should I do? 我该怎么办? Could anybody tell me how to use canvas API in fabric.js? 有人可以告诉我如何在fabric.js中使用canvas API吗?

I do not understand contextTop, contextContainer... 我不了解contextTop,contextContainer ...

You can't use canvas API directly. 您不能直接使用canvas API。
FabricJS is a framework to work on the canvas and you have to use the framework rules, or it will break. FabricJS是一个可以在画布上工作的框架,您必须使用框架规则,否则它将中断。

ContextTop is the result of getContext('2d') from the upperCanvas, where the upperCanvas is a canvas layered on top of the main one to do some quick drawing that does not require full canvas refresh. ContextTop是upperCanvas中getContext('2d')的结果,其中up​​perCanvas是一个位于主画布之上的画布,可以进行一些不需要完全刷新画布的快速绘制。

All the html5 canvas drawing that are done outside the provided API will be wiped at the next frame. 在提供的API之外完成的所有html5画布绘制都将在下一帧擦除。

To draw a rect you have to create the rect class and add it to the canvas, that is the correct way to do it: 要绘制一个rect,您必须创建rect类并将其添加到画布,这是正确的方法:

var rect = new fabric.Rect({ width: 200, height: 300 });
canvas.add(rect);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM