I want use fabricjs in my project. I installed fabricjs using bower and linked in index.html. but it is not working. Please see the below code.
index.html
<html>
<head>
<script>document.write('<base href="' + document.location + '" />'); </script>
<title>Image Editor</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="css/bootstrap/dist/css/bootstrap.min.css">
<!-- 1. Load libraries -->
<!-- Polyfill(s) for older browsers -->
<script src="libs/core-js/client/shim.min.js"></script>
<script src="libs/zone.js/dist/zone.js"></script>
<script src="libs/reflect-metadata/Reflect.js"></script>
<script src="libs/systemjs/dist/system.src.js"></script>
<script src="css/jquery/dist/jquery.min.js"></script>
<script src="css/bootstrap/dist/js/bootstrap.min.js"></script>
// incuding fabricjs here
<script src="../../bower_components/fabric.js/dist/fabric.min.js"></script>
<!-- 2. Configure SystemJS -->
<script src="systemjs.config.js"></script>
<script>
System.import('app').catch(function(err){ console.error(err); });
</script>
</head>
<!-- 3. Display the application -->
<body>
<my-app>Loading...</my-app>
</body>
</html>
This is component file "stage.component.ts"
import {Component, OnInit} from '@angular/core';
import { ActivatedRoute, Router} from '@angular/router';
import { fabric } from 'fabric';
import { ImageService } from '../services/image.service';
declare var fabric:any;
@Component({
selector:'my-stage',
styleUrls: ['./app/stage/stage.component.css'],
templateUrl: './app/stage/stage.component.html',
})
export class StageComponent implements OnInit {
title:string = 'Image Editor';
imgpath: string = '';
canvas:any = null;
// fabric:any = null;
constructor(
private route: ActivatedRoute,
) {
// this.fabric = new fabric();
}
ngOnInit() {
this.canvas = new fabric.Canvas('fstage', {
isDrawingMode: true,
selection: true
});
}
}
I have search many reference but couldn't find out what is wrong with my. Please advice, thank you.
I also have same problem, following is my solution.
Install fabric
$ npm install fabric --save
Generate typings module
$ typings install fabric --source dt --save
Put
declare module 'fabric';
at the end ofsrc/typings.d.ts
import { fabric } from 'fabric';
in the Angular2 Component.It is working fine and giving fabric autocomplete suggestions also.
In this way I added Fabric.js to my Angular project created using angular-cli:
1) Install Cairo on your system.
Note: 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.
2) Install Fabric.js
3) Install TypeScript definitions for Fabric.js
4) Import fabric in required ts file
Here you can find my "Hello World" Angular + Fabric.js project.
Other links:
Fabricjs website
Fabricjs docs
Fabric and type definitions
I have similar issue recently, the following steps resolved it. (angular 2.3.1 + angular-cli 1.0.0-beta.31)
src
folder.declare module 'fabric';
in it.import { fabric } from 'fabric';
in your Angular 2 components.I can use fabric.js without the line
loading the script on index.html and declaring the library on the ts file:
Out on browser console:
Modern usage:
npm i fabric
npm i @types/fabric
Demo with Angular 5.2.8 and Fabric 2.2.2
HTML
TypeScript