Skip to content

Commit 82feb34

Browse files
- Download Image , Backgroud , Height , Width, Background and Pan Setting
- Upload File and Process Data. - State Changes - Process of implemented .
1 parent 1a3ec83 commit 82feb34

File tree

3 files changed

+53
-48
lines changed

3 files changed

+53
-48
lines changed

examples/main.jsx

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,8 @@ class SketchFieldDemo extends React.Component {
136136
canUndo: false,
137137
canRedo: false,
138138
controlledSize: false,
139-
sketchWidth: 800,
140-
sketchHeight: 600,
139+
sketchWidth: (window.innerWidth - 400),
140+
sketchHeight: (window.innerHeight - 200),
141141
stretched: true,
142142
stretchedX: false,
143143
stretchedY: false,
@@ -154,7 +154,9 @@ class SketchFieldDemo extends React.Component {
154154
enableCopyPaste: false,
155155
isOpen: true,
156156
dataJson: dataJson,
157-
url: dataUrl
157+
url: dataUrl,
158+
maxHeight: (window.innerHeight + 800),
159+
isCanvas: false
158160
};
159161
}
160162

@@ -191,6 +193,7 @@ class SketchFieldDemo extends React.Component {
191193
var url = canvas.backgroundImage._element.src;
192194
this.setState({url: url})
193195
}
196+
194197
this.setState({isOpen: true , objects: objects});
195198
};
196199

@@ -360,19 +363,31 @@ class SketchFieldDemo extends React.Component {
360363
_addText = () => this._sketch.addText(this.state.text);
361364

362365
_readFile = (event) => {
363-
let sketch = this._sketch;
364-
let file = event.target.files[0]
366+
367+
this._clear();
368+
let file = event.target.files[0];
369+
365370
if(file){
366371
let reader = new FileReader();
367-
reader.onload = function (e) {
368-
let data = e.target.result;
369-
console.log(data);
370-
sketch.fromJSON(data);
371-
}
372+
reader.addEventListener(
373+
'load',
374+
() => this._loadData(reader.result), false,
375+
);
372376
reader.readAsText(file);
373377
}
374378
}
375379

380+
_loadData = (data) =>{
381+
data = JSON.parse(data);
382+
this.setState({height: data.height, width: data.width})
383+
let sketch = this._sketch;
384+
sketch.fromJSON(data);
385+
sketch._backgroundColor(data.backgroundColor);
386+
if(data.backImg){
387+
sketch.changeBackground(data.backImg)
388+
}
389+
}
390+
376391
componentDidMount = () => {
377392
(function(console) {
378393
console.save = function(data, filename) {
@@ -449,6 +464,7 @@ class SketchFieldDemo extends React.Component {
449464
onClick={this._save}>
450465
<SaveIcon/>
451466
</IconButton>
467+
452468
<Button
453469
style={{background: 'none', border: 'none', color: '#607d8b', boxShadow: 'none'}}
454470
variant="contained"
@@ -458,7 +474,7 @@ class SketchFieldDemo extends React.Component {
458474
<input type="file"
459475
onChange={(e) => this._readFile(e)}
460476
style={{ display: "none" }} />
461-
</Button>
477+
</Button>
462478
<IconButton
463479
color="primary"
464480
title="Download"
@@ -479,7 +495,6 @@ class SketchFieldDemo extends React.Component {
479495
<div className="row">
480496

481497
<div className="col-xs-7 col-sm-7 col-md-9 col-lg-9">
482-
483498
<SketchField
484499
name="sketch"
485500
className="canvas-area"
@@ -498,10 +513,10 @@ class SketchFieldDemo extends React.Component {
498513
: 'transparent'
499514
}
500515
width={
501-
this.state.controlledSize ? this.state.sketchWidth : null
516+
this.state.controlledSize ? this.state.sketchWidth : this.state.width
502517
}
503518
height={
504-
this.state.controlledSize ? this.state.sketchHeight : null
519+
this.state.controlledSize ? this.state.sketchHeight : this.state.height
505520
}
506521
defaultValue={this.state.dataJson}
507522
value={controlledValue}
@@ -510,6 +525,7 @@ class SketchFieldDemo extends React.Component {
510525
tool={this.state.tool}
511526
/>
512527
</div>
528+
513529
<div className="col-xs-5 col-sm-5 col-md-3 col-lg-3">
514530
<Card style={styles.card}>
515531
<CardHeader

src/SketchField.jsx

Lines changed: 17 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,14 @@ class SketchField extends PureComponent {
304304
canvas.calcOffset();
305305
};
306306

307+
_canvansSize = () =>{
308+
let canvas = this._fc;
309+
console.log(canvas);
310+
canvas.height = 500;
311+
canvas.renderAll()
312+
console.log(canvas)
313+
}
314+
307315
/**
308316
* Sets the background color for this sketch
309317
* @param color in rgba or hex format
@@ -460,7 +468,6 @@ class SketchField extends PureComponent {
460468

461469
copy = () => {
462470
let canvas = this._fc;
463-
464471
};
465472

466473
paste = () => {
@@ -628,33 +635,17 @@ class SketchField extends PureComponent {
628635
* Download Content
629636
*/
630637
download = () => {
631-
this._history.saveToFile();
632-
}
633-
634-
/**
635-
* Save Current Index
636-
*/
637-
638-
save = () => {
639-
let canvas = this._fc;
640-
let objects = canvas.getObjects();
641-
let stObjects = JSON.stringify(objects)
642-
this.setState({save: stObjects})
638+
var url = null;
639+
if(this._fc.backgroundImage){
640+
url = this._fc.backgroundImage._element.src;
641+
}
642+
let hash = { height: this._fc.height, width: this._fc.width,
643+
backgroundColor: this._fc.backgroundColor, viewportTransform: this._fc.viewportTransform,
644+
backImg: url,
645+
}
646+
this._history.saveToFile(hash);
643647
}
644648

645-
/**
646-
* Open Current Saved
647-
*/
648-
649-
open = () => {
650-
let canvas = this._fc;
651-
652-
let objects = this.state.save;
653-
654-
655-
656-
this.setState({isView: true})
657-
}
658649

659650
render = () => {
660651
let { className, style, width, height } = this.props;

src/history.js

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -82,20 +82,16 @@ class History {
8282
* @returns the new current value after the redo operation, or null if no redo operation was possible
8383
*/
8484
redo() {
85-
console.log('heres')
85+
8686
try {
87-
console.log('checking is here')
88-
console.log(this.redoList)
8987
if (this.redoList.length > 0) {
90-
9188
if (this.current) this.undoList.push(this.current);
9289
this.current = this.redoList.pop();
9390
return this.current;
9491
}
95-
9692
return null;
9793
} finally {
98-
//this.print();
94+
this.print();
9995
}
10096
}
10197

@@ -132,7 +128,7 @@ class History {
132128
*
133129
*
134130
*/
135-
saveToFile() {
131+
saveToFile(hash) {
136132
let dataArray = [];
137133

138134
if(this.current){
@@ -142,11 +138,13 @@ class History {
142138
} else {
143139
for(let i = 0; i < this.undoList.length; i++){
144140
dataArray.push(this.undoList[i][0])
141+
dataArray.push(this.current[0])
145142
}
146143
}
147144
}
148145

149-
var hash = {"objects": dataArray}
146+
var hash1 = {"objects": dataArray}
147+
hash["objects"] = dataArray;
150148
const a = document.createElement("a");
151149
a.href = URL.createObjectURL(new Blob([JSON.stringify(hash, null, 4)], {
152150
type: "text/plain"

0 commit comments

Comments
 (0)