UNPKG

14.1 kBMarkdownView Raw
1### Fabric
2
3<!-- chat, support -->
4
5[![Gitter](https://badges.gitter.im/JoinChat.svg)](https://gitter.im/kangax/fabric.js?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
6
7<!-- build/coverage status, climate -->
8
9[![Build Status](https://secure.travis-ci.org/kangax/fabric.js.svg?branch=master)](http://travis-ci.org/#!/kangax/fabric.js)
10[![Code Climate](https://d3s6mut3hikguw.cloudfront.net/github/kangax/fabric.js/badges/gpa.svg)](https://codeclimate.com/github/kangax/fabric.js)
11[![Coverage Status](https://coveralls.io/repos/kangax/fabric.js/badge.png?branch=master)](https://coveralls.io/r/kangax/fabric.js?branch=master)
12
13<!-- npm, bower, CDNJS versions, downloads -->
14
15[![Bower version](https://badge.fury.io/bo/fabric.svg)](http://badge.fury.io/bo/fabric)
16[![NPM version](https://badge.fury.io/js/fabric.svg)](http://badge.fury.io/js/fabric)
17[![Downloads per month](https://img.shields.io/npm/dm/fabric.svg)](https://www.npmjs.org/package/fabric)
18[![CDNJS version](https://img.shields.io/cdnjs/v/fabric.js.svg)](https://cdnjs.com/libraries/fabric.js)
19
20<!-- deps status -->
21
22[![Dependency Status](https://david-dm.org/kangax/fabric.js.svg?theme=shields.io)](https://david-dm.org/kangax/fabric.js)
23[![devDependency Status](https://david-dm.org/kangax/fabric.js/dev-status.svg?theme=shields.io)](https://david-dm.org/kangax/fabric.js#info=devDependencies)
24
25<!-- bounties, tips -->
26
27[![Bountysource](https://api.bountysource.com/badge/tracker?tracker_id=23217)](https://www.bountysource.com/trackers/23217-fabric-js?utm_source=23217&utm_medium=shield&utm_campaign=TRACKER_BADGE)
28[![Flattr this git repo](http://api.flattr.com/button/flattr-badge-large.png)](https://flattr.com/submit/auto?user_id=kangax&url=http://github.com/kangax/fabric.js&title=Fabric.js&language=&tags=github&category=software)
29
30**Fabric.js** is a framework that makes it easy to work with HTML5 canvas element. It is an **interactive object model** on top of canvas element. It is also an **SVG-to-canvas parser**.
31
32<a href="http://fabricjs.com/kitchensink" target="_blank"><img src="https://github.com/kangax/fabric.js/raw/master/lib/screenshot.png" style="width:300px;box-shadow:rgba(0,0,0,0.3) 0 0 5px"></a>
33
34Using Fabric.js, you can create and populate objects on canvas; objects like simple geometrical shapes — rectangles, circles, ellipses, polygons, or more complex shapes consisting of hundreds or thousands of simple paths. You can then scale, move, and rotate these objects with the mouse; modify their properties — color, transparency, z-index, etc. You can also manipulate these objects altogether — grouping them with a simple mouse selection.
35
36### Non-Technical Introduction to Fabric
37
38Fabric.js allows you to easily create simple shapes like rectangles, circles, triangles and other polygons or more complex shapes made up of many paths, onto the HTML `<canvas>` element on a webpage using JavaScript. Fabric.js will then allow you to manipulate the size, position and rotation of these objects with a mouse. It’s also possible to change some of the attributes of these objects such as their color, transparency, depth position on the webpage or selecting groups of these objects using the Fabric.js library. Fabric.js will also allow you to convert an SVG image into JavaScript data that can be used for putting it onto the `<canvas>` element.
39
40
41[Contributions](https://github.com/kangax/fabric.js/wiki/Love-Fabric%3F-Help-us-by...) are very much welcome!
42
43### Goals
44
45- Unit tested (1150+ tests at the moment, 79%+ coverage)
46- Modular (~60 small ["classes", modules, mixins](http://fabricjs.com/docs/))
47- Cross-browser
48- [Fast](https://github.com/kangax/fabric.js/wiki/Focus-on-speed)
49- Encapsulated in one object
50- No browser sniffing for critical functionality
51- Runs under ES5 strict mode
52- Runs on a server under [Node.js](http://nodejs.org/) (active stable releases and latest of current) (see [Node.js limitations](https://github.com/kangax/fabric.js/wiki/Fabric-limitations-in-node.js))
53- Follows [Semantic Versioning](http://semver.org/)
54
55### Supported browsers
56
57- Firefox 2+
58- Safari 3+
59- Opera 9.64+
60- Chrome (all versions)
61- IE10, IE11, Edge
62
63You can [run automated unit tests](http://fabricjs.com/test/unit/) right in the browser.
64
65### History
66
67Fabric.js started as a foundation for design editor on [printio.ru](http://printio.ru) — interactive online store with ability to create your own designs. The idea was to create [Javascript-based editor](http://printio.ru/ringer_man_tees/new), which would make it easy to manipulate vector shapes and images on T-Shirts. Since performance was one of the most critical requirements, we chose canvas over SVG. While SVG is excellent with static shapes, it's not as performant as canvas when it comes to dynamic manipulation of objects (movement, scaling, rotation, etc.). Fabric.js was heavily inspired by [Ernest Delgado's canvas experiment](http://www.ernestdelgado.com/public-tests/canvasphoto/demo/canvas.html). In fact, code from Ernest's experiment was the foundation of an entire framework. Later, Fabric.js grew into a collection of distinct object types and got an SVG-to-canvas parser.
68
69<h3 id="bower-install">Install with bower</h3>
70
71 $ bower install fabric
72
73<h3 id="npm-install">Install with npm</h3>
74
75To install Fabric.js using npm, you must first manually [install Cairo](http://cairographics.org/download/) on your system. Cairo is a system library which powers node-canvas, which Fabric.js relies on. When the installation is complete, you may need to restart your terminal or command prompt before installing fabric.
76
77 $ npm install fabric --save
78
79<h3 id="fabric-building">Building</h3>
80
811. [Install Node.js](https://github.com/joyent/node/wiki/Installation)
82
832. Build distribution file **[~77K minified, ~20K gzipped]**
84
85 $ node build.js
86
87 2.1 Or build a custom distribution file, by passing (comma separated) module names to be included.
88
89 $ node build.js modules=text,serialization,parser
90 // or
91 $ node build.js modules=text
92 // or
93 $ node build.js modules=parser,text
94 // etc.
95
96 By default (when none of the modules are specified) only basic functionality is included.
97 See the list of modules below for more information on each one of them.
98 Note that default distribution has support for **static canvases** only.
99
100 To get minimal distribution with interactivity, make sure to include corresponding module:
101
102 $ node build.js modules=interaction
103
104 2.2 You can also include all modules like so:
105
106 $ node build.js modules=ALL
107
108 2.3 You can exclude a few modules like so:
109
110 $ node build.js modules=ALL exclude=gestures,image_filters
111
1123. Create a minified distribution file
113
114 # Using YUICompressor (default option)
115 $ node build.js modules=... minifier=yui
116
117 # or Google Closure Compiler
118 $ node build.js modules=... minifier=closure
119
1204. Enable AMD support via require.js (requires uglify)
121
122 $ node build.js requirejs modules=...
123
1245. Create source map file for better productive debugging (requires uglify or google closure compiler).<br>More information about [source maps](http://www.html5rocks.com/en/tutorials/developertools/sourcemaps/).
125
126 $ node build.js sourcemap modules=...
127
128 If you use google closure compiler you have to add `sourceMappingURL` manually at the end of the minified file all.min.js (see issue https://code.google.com/p/closure-compiler/issues/detail?id=941).
129
130 //# sourceMappingURL=fabric.min.js.map
131
1326. Ensure code guidelines are met (prerequisite: `npm -g install eslint`)
133
134 $ npm run lint && npm run lint_tests
135
136<h3 id="fabric-building">Testing</h3>
137
1381. [Install Node.js](https://github.com/joyent/node/wiki/Installation)
139
1402. [Install NPM, if necessary](https://github.com/npm/npm#super-easy-install)
141
1423. Install NPM packages
143
144 $ npm install
145
1464. Run test suite
147
148Make sure testem is installed
149
150 $ npm install -g testem
151
152Run tests Chrome and Node (by default):
153
154 $ testem
155
156See testem docs for more info: https://github.com/testem/testem
157
158### Demos
159
160- [Demos](http://fabricjs.com/demos/)
161- [Kitchensink demo](http://fabricjs.com/kitchensink)
162- [Benchmarks](http://fabricjs.com/benchmarks/)
163
164[Who's using Fabric?](http://trends.builtwith.com/javascript/FabricJS)
165
166### Documentation
167
168Documentation is always available at [http://fabricjs.com/docs/](http://fabricjs.com/docs/).
169
170Also see [official 4-part intro series](http://fabricjs.com/articles), [presentation from BK.js](http://www.slideshare.net/kangax/fabricjs-building-acanvaslibrarybk) and [presentation from Falsy Values](http://www.slideshare.net/kangax/fabric-falsy-values-8067834) for an overview of fabric.js, how it works, and its features.
171
172### Optional modules
173
174These are the optional modules that could be specified for inclusion, when building custom version of fabric:
175
176- **text** — Adds support for static text (`fabric.Text`)
177- **itext** — Adds support for interactive text (`fabric.IText`, `fabric.Textbox`)
178- **serialization** — Adds support for `loadFromJSON`, `loadFromDatalessJSON`, and `clone` methods on `fabric.Canvas`
179- **interaction** — Adds support for interactive features of fabric — selecting/transforming objects/groups via mouse/touch devices.
180- **parser** — Adds support for `fabric.parseSVGDocument`, `fabric.loadSVGFromURL`, and `fabric.loadSVGFromString`
181- **image_filters** — Adds support for image filters, such as grayscale of white removal.
182- **easing** — Adds support for animation easing functions
183- **node** — Adds support for running fabric under node.js, with help of [jsdom](https://github.com/tmpvar/jsdom) and [node-canvas](https://github.com/learnboost/node-canvas) libraries.
184- **freedrawing** — Adds support for free drawing
185- **gestures** — Adds support for multitouch gestures with help of [Event.js](https://github.com/mudcube/Event.js)
186- **object_straightening** — Adds support for rotating an object to one of 0, 90, 180, 270, etc. depending on which is angle is closer.
187- **animation** — Adds support for animation (`fabric.util.animate`, `fabric.util.requestAnimFrame`, `fabric.Object#animate`, `fabric.Canvas#fxCenterObjectH/#fxCenterObjectV/#fxRemove`)
188
189Additional flags for build script are:
190
191- **requirejs** — Makes fabric requirejs AMD-compatible in `dist/fabric.js`. *Note:* an unminified, requirejs-compatible version is always created in `dist/fabric.require.js`
192- **no-strict** — Strips "use strict" directives from source
193- **no-svg-export** — Removes svg exporting functionality
194- **sourcemap** - Generates a sourceMap file and adds the `sourceMappingURL` (only if uglifyjs is used) to `dist/fabric.min.js`
195
196For example:
197
198 node build.js modules=ALL exclude=json no-strict no-svg-export
199
200### Examples of use
201
202#### Adding red rectangle to canvas
203
204```html
205<!DOCTYPE html>
206<html>
207<head>
208</head>
209<body>
210 <canvas id="canvas" width="300" height="300"></canvas>
211
212 <script src="lib/fabric.js"></script>
213 <script>
214 var canvas = new fabric.Canvas('canvas');
215
216 var rect = new fabric.Rect({
217 top : 100,
218 left : 100,
219 width : 60,
220 height : 70,
221 fill : 'red'
222 });
223
224 canvas.add(rect);
225 </script>
226</body>
227</html>
228```
229
230### Helping Fabric
231
232- [Fabric on Bountysource](https://www.bountysource.com/trackers/23217-fabric-js)
233- [Fabric on CodeTriage](http://www.codetriage.com/kangax/fabric.js)
234
235### Staying in touch
236
237Follow [@fabric.js](http://twitter.com/fabricjs), [@kangax](http://twitter.com/kangax) or [@AndreaBogazzi](http://twitter.com/AndreaBogazzi) on twitter.
238
239Questions, suggestions — [fabric.js on Google Groups](http://groups.google.com/group/fabricjs).
240
241See [Fabric questions on Stackoverflow](http://stackoverflow.com/questions/tagged/fabricjs),
242Fabric snippets on [jsfiddle](http://jsfiddle.net/user/fabricjs/fiddles/)
243or [codepen.io](http://codepen.io/tag/fabricjs).
244
245Fabric on [LibKnot](http://libknot.ohmztech.com/).
246
247Get help in Fabric's IRC channel — irc://irc.freenode.net/#fabric.js
248
249### Credits
250
251- [Andrea Bogazzi](https://twitter.com/AndreaBogazzi) for help with bugs, new features, documentation, GitHub issues
252- Ernest Delgado for the original idea of [manipulating images on canvas](http://www.ernestdelgado.com/archive/canvas/)
253- [Maxim "hakunin" Chernyak](http://twitter.com/hakunin) for ideas, and help with various parts of the library throughout its life
254- [Sergey Nisnevich](http://nisnya.com) for help with geometry logic
255- [Stefan Kienzle](https://twitter.com/kienzle_s) for help with bugs, features, documentation, GitHub issues
256- [Shutterstock](http://www.shutterstock.com/jobs) for the time and resources invested in using and improving fabric.js
257- [And all the other GitHub contributors](https://github.com/kangax/fabric.js/graphs/contributors)
258
259### MIT License
260
261Copyright (c) 2008-2015 Printio (Juriy Zaytsev, Maxim Chernyak)
262
263Permission is hereby granted, free of charge, to any person obtaining a copy
264of this software and associated documentation files (the "Software"), to deal
265in the Software without restriction, including without limitation the rights
266to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
267copies of the Software, and to permit persons to whom the Software is
268furnished to do so, subject to the following conditions:
269
270THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
271IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
272FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
273AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
274LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
275OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
276SOFTWARE.
277
278
279[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/kangax/fabric.js/trend.png)](https://bitdeli.com/free "Bitdeli Badge")