Skip to content

Commit 8785534

Browse files
committed
addText Functionality
1 parent 0de33a2 commit 8785534

File tree

1 file changed

+29
-8
lines changed

1 file changed

+29
-8
lines changed

src/SketchField.jsx

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
/*eslint no-unused-vars: 0*/
22
'use strict';
33

4-
import React, {PureComponent} from 'react'
4+
import React, { PureComponent } from 'react'
55
import PropTypes from 'prop-types'
66
import History from './history'
7-
import {uuid4} from './utils'
7+
import { uuid4 } from './utils'
88
import Select from './select'
99
import Pencil from './pencil'
1010
import Line from './line'
@@ -124,12 +124,28 @@ class SketchField extends PureComponent {
124124
});
125125
};
126126

127+
addText = (text, options = {}) => {
128+
let canvas = this._fc;
129+
let iText = new fabric.IText(text, options);
130+
let opts = {
131+
left: (canvas.getWidth() - iText.width) * 0.5,
132+
top: (canvas.getHeight() - iText.height) * 0.5,
133+
};
134+
Object.assign(options, opts);
135+
iText.set({
136+
'left': options.left,
137+
'top': options.top
138+
})
139+
140+
canvas.add(iText);
141+
}
142+
127143
/**
128144
* Action when an object is added to the canvas
129145
*/
130146
_onObjectAdded = (e) => {
131147
if (!this.state.action) {
132-
this.setState({action: true});
148+
this.setState({ action: true });
133149
return
134150
}
135151
let obj = e.target;
@@ -206,9 +222,9 @@ class SketchField extends PureComponent {
206222
*/
207223
_resize = (e) => {
208224
if (e) e.preventDefault();
209-
let {widthCorrection, heightCorrection} = this.props;
225+
let { widthCorrection, heightCorrection } = this.props;
210226
let canvas = this._fc;
211-
let {offsetWidth, clientHeight} = this._container;
227+
let { offsetWidth, clientHeight } = this._container;
212228
let prevWidth = canvas.getWidth();
213229
let prevHeight = canvas.getHeight();
214230
let wfactor = ((offsetWidth - widthCorrection) / prevWidth).toFixed(2);
@@ -308,7 +324,7 @@ class SketchField extends PureComponent {
308324
//noinspection Eslint
309325
let [obj, prevState, currState] = history.redo();
310326
if (obj.version === 0) {
311-
this.setState({action: false}, () => {
327+
this.setState({ action: false }, () => {
312328
canvas.add(obj);
313329
obj.version = 1
314330
})
@@ -474,6 +490,11 @@ class SketchField extends PureComponent {
474490
canvas.on('object:moving', this._onObjectMoving);
475491
canvas.on('object:scaling', this._onObjectScaling);
476492
canvas.on('object:rotating', this._onObjectRotating);
493+
// IText Events fired on Adding Text
494+
// canvas.on("text:event:changed", console.log)
495+
// canvas.on("text:selection:changed", console.log)
496+
// canvas.on("text:editing:entered", console.log)
497+
// canvas.on("text:editing:exited", console.log)
477498

478499
this.disableTouchScroll();
479500

@@ -554,8 +575,8 @@ class SketchField extends PureComponent {
554575
} = this.props;
555576

556577
let canvasDivStyle = Object.assign({}, style ? style : {},
557-
width ? {width: width} : {},
558-
height ? {height: height} : {height: 512});
578+
width ? { width: width } : {},
579+
height ? { height: height } : { height: 512 });
559580

560581
return (
561582
<div

0 commit comments

Comments
 (0)