Skip to content

Commit 54febd2

Browse files
author
Thomas Bolis
committed
Improving Examples
1 parent 0a524d1 commit 54febd2

File tree

3 files changed

+83
-61
lines changed

3 files changed

+83
-61
lines changed

examples/main.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ body, html {
1515
min-height: 100%;
1616
width: 100%;
1717
height: 100%;
18-
background-color: beige;
18+
background-color: #eeffd3;
1919
color: #000000;
2020
}
2121

examples/main.jsx

Lines changed: 79 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/*eslint no-unused-vars: 0*/
12
'use strict';
23

34
import React from 'react';
@@ -10,14 +11,16 @@ import {
1011
Card,CardText,CardTitle,
1112
AppBar,GridList,GridTile,
1213
Slider, Toggle, MenuItem,
13-
SelectField, IconButton
14+
SelectField, IconButton,
15+
ToolbarSeparator
1416
} from 'material-ui';
1517

1618
import UndoIcon from 'material-ui/lib/svg-icons/content/undo';
1719
import RedoIcon from 'material-ui/lib/svg-icons/content/redo';
1820
import ClearIcon from 'material-ui/lib/svg-icons/action/delete';
1921
import SaveIcon from 'material-ui/lib/svg-icons/content/save';
2022
import RemoveIcon from 'material-ui/lib/svg-icons/content/clear';
23+
import DownloadIcon from 'material-ui/lib/svg-icons/file/file-download';
2124

2225
import dataJson from './data.json'
2326
import dataUrl from './data.url'
@@ -30,31 +33,53 @@ const styles = {
3033
padding: '3px',
3134
display: 'flex',
3235
flexWrap: 'wrap',
36+
margin: '10px 10px 5px 10px',
3337
justifyContent: 'space-around'
3438
},
3539
gridList: {
3640
width: '100%',
37-
height: 400,
3841
overflowY: 'auto',
3942
marginBottom: 24
4043
},
4144
gridTile: {
4245
backgroundColor: '#fcfcfc'
46+
},
47+
appBar: {
48+
backgroundColor: '#333'
49+
},
50+
separator: {
51+
height: '42px',
52+
backgroundColor: 'white'
53+
},
54+
iconButton: {
55+
fill: 'white',
56+
width: '42px',
57+
height: '42px'
4358
}
4459
};
4560

61+
function eventFire(el, etype) {
62+
if (el.fireEvent) {
63+
el.fireEvent('on' + etype);
64+
} else {
65+
var evObj = document.createEvent('Events');
66+
evObj.initEvent(etype, true, false);
67+
el.dispatchEvent(evObj);
68+
}
69+
}
70+
4671
class SketchFieldDemo extends React.Component {
4772
constructor(params) {
4873
super(params);
4974

5075
this._save = this._save.bind(this);
51-
this._selectTool = this._selectTool.bind(this);
52-
this._renderTile = this._renderTile.bind(this);
53-
this._removeMe = this._removeMe.bind(this);
5476
this._undo = this._undo.bind(this);
5577
this._redo = this._redo.bind(this);
5678
this._clear = this._clear.bind(this);
57-
this._toggleEdit = this._toggleEdit.bind(this);
79+
this._removeMe = this._removeMe.bind(this);
80+
this._download = this._download.bind(this);
81+
this._renderTile = this._renderTile.bind(this);
82+
this._selectTool = this._selectTool.bind(this);
5883

5984
this.state = {
6085
lineColor: 'black',
@@ -70,33 +95,34 @@ class SketchFieldDemo extends React.Component {
7095
};
7196
}
7297

98+
7399
componentDidMount() {
74-
(function (console) {
75100

76-
console.save = function (data, filename) {
101+
/*eslint-disable no-console*/
77102

103+
(function (console) {
104+
console.save = function (data, filename) {
78105
if (!data) {
79-
console.error('Console.save: No data')
106+
console.error('Console.save: No data');
80107
return;
81108
}
82-
83-
if (!filename) filename = 'console.json'
84-
85-
if (typeof data === "object") {
109+
if (!filename) filename = 'console.json';
110+
if (typeof data === 'object') {
86111
data = JSON.stringify(data, undefined, 4)
87112
}
88-
89113
var blob = new Blob([data], {type: 'text/json'}),
90114
e = document.createEvent('MouseEvents'),
91-
a = document.createElement('a')
92-
93-
a.download = filename
94-
a.href = window.URL.createObjectURL(blob)
95-
a.dataset.downloadurl = ['text/json', a.download, a.href].join(':')
96-
e.initMouseEvent('click', true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null)
115+
a = document.createElement('a');
116+
a.download = filename;
117+
a.href = window.URL.createObjectURL(blob);
118+
a.dataset.downloadurl = ['text/json', a.download, a.href].join(':');
119+
e.initMouseEvent('click', true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
97120
a.dispatchEvent(e)
98121
}
99-
})(console)
122+
})(console);
123+
124+
/*eslint-enable no-console*/
125+
100126
}
101127

102128
_selectTool(event, index, value) {
@@ -106,9 +132,25 @@ class SketchFieldDemo extends React.Component {
106132
}
107133

108134
_save() {
109-
//let drawings = this.state.drawings;
110-
//drawings.push(this._sketch.getContent());
111-
//this.setState({drawings: drawings});
135+
let drawings = this.state.drawings;
136+
drawings.push(this._sketch.toDataURL());
137+
this.setState({drawings: drawings});
138+
}
139+
140+
_download() {
141+
/*eslint-disable no-console*/
142+
143+
console.save(this._sketch.toDataURL(), 'toDataURL.txt');
144+
console.save(JSON.stringify(this._sketch.toJSON()), 'toDataJSON.txt');
145+
146+
/*eslint-enable no-console*/
147+
148+
let {imgDown} = this.refs;
149+
let event = new Event('click', {});
150+
151+
imgDown.href = this._sketch.toDataURL();
152+
imgDown.download = 'toPNG.png';
153+
imgDown.dispatchEvent(event);
112154
}
113155

114156
_renderTile(drawing, index) {
@@ -150,35 +192,17 @@ class SketchFieldDemo extends React.Component {
150192

151193
_clear() {
152194
this._sketch.clear();
153-
this.setState({
154-
canUndo: this._sketch.canUndo(),
155-
canRedo: this._sketch.canRedo()
156-
})
157195
}
158196

159-
_toggleEdit = (event, toggled) => {
160-
this.setState({
161-
drawingMode: !toggled
162-
});
163-
};
164-
165197
render() {
166-
167-
let styles = {
168-
iconButton: {
169-
width: '42px',
170-
height: '42px'
171-
}
172-
};
173-
174198
return (
175199
<div>
176200

177201
{/* Application Bar with tools */}
178202

179203
<div className='row'>
180204
<div className='col-xs-12 col-sm-12 col-md-12 col-lg-12'>
181-
<AppBar title='Sketch Tool' showMenuIconButton={false}>
205+
<AppBar title='Sketch Tool' showMenuIconButton={false} style={styles.appBar}>
182206
<IconButton
183207
onTouchTap={this._undo}
184208
iconStyle={styles.iconButton}>
@@ -190,18 +214,23 @@ class SketchFieldDemo extends React.Component {
190214
disabled={!this.state.canRedo}>
191215
<RedoIcon/>
192216
</IconButton>
193-
&nbsp;
194-
&nbsp;
195-
&nbsp;
217+
<ToolbarSeparator style={styles.separator}/>
196218
<IconButton
197-
onTouchTap={this._clear}
219+
onTouchTap={this._save}
198220
iconStyle={styles.iconButton}>
199-
<ClearIcon />
221+
<SaveIcon />
200222
</IconButton>
201223
<IconButton
202-
onTouchTap={this._save}
224+
onTouchTap={this._download}
203225
iconStyle={styles.iconButton}>
204-
<SaveIcon />
226+
<DownloadIcon />
227+
</IconButton>
228+
<a ref='imgDown'/>
229+
<ToolbarSeparator style={styles.separator}/>
230+
<IconButton
231+
onTouchTap={this._clear}
232+
iconStyle={styles.iconButton}>
233+
<ClearIcon />
205234
</IconButton>
206235
</AppBar>
207236
</div>
@@ -233,13 +262,6 @@ class SketchFieldDemo extends React.Component {
233262
</div>
234263
</div>
235264
<div className='col-xs-5 col-sm-5 col-md-3 col-lg-3'>
236-
{/*<Card style={{margin:'5px 10px 5px 0'}}>
237-
<CardTitle title='Options'/>
238-
<CardText>
239-
<Toggle label="Edit" onToggle={this._toggleEdit}/>
240-
</CardText>
241-
</Card>*/}
242-
243265
<Card style={{margin:'10px 10px 5px 0'}}>
244266
<CardTitle title='Tools'/>
245267
<CardText>

src/SketchField.jsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -236,12 +236,12 @@ class SketchField extends React.Component {
236236
obj.top = tempTop;
237237
obj.setCoords();
238238
}
239-
canvas.renderAll();
240-
canvas.calcOffset();
241239
this.setState({
242240
parentWidth: width,
243241
parentHeight: height
244242
});
243+
canvas.renderAll();
244+
canvas.calcOffset();
245245
}
246246

247247
/**
@@ -368,7 +368,7 @@ class SketchField extends React.Component {
368368
* Clear the content of the canvas, this will also keep the last version of it to history
369369
*/
370370
clear() {
371-
this._fc.clear()
371+
this._fc.clear();
372372
}
373373

374374
render() {

0 commit comments

Comments
 (0)