@@ -2,55 +2,58 @@ import React from 'react'
2
2
import { useSpring , animated } from 'react-spring'
3
3
import { fabric } from "fabric"
4
4
5
- // this was very helpful in figuring out how to get fabric in react https://codesandbox.io/s/react-fabric-example-87hh4
5
+ const Decorate = ( { stage , setStage , photoTaken , photoTakenEncoded , dimensions } ) => {
6
6
7
+ const opacity = useSpring ( { opacity : 1 , from : { opacity : 0 } } ) ;
7
8
8
- const Decorate = ( { stage, setStage, photoTaken, photoTakenEncoded, aspectRatio, ...props } ) => {
9
- // I would suggest against calling a variable "props", that's sort of reserved.
10
- const opacity = useSpring ( { opacity : 1 , from : { opacity : 0 } } ) ;
11
- // Also note my use of "...props" above. Now there is a variable called props which is
12
- // an object that contains anything we didn't destructure above.
13
- // ex. if you had called this component thusly: <Decorate stage={something} setStage={somethingElse} herp="derp" hoop="doop" />
14
- // `props` would equal { herp: 'derp', hoop: 'doop' }
15
-
16
9
// Set up a persistent canvas
17
10
let canvas ;
18
11
12
+ const updateState = ( ) => {
13
+ canvas . discardActiveObject ( ) ;
14
+ const filteredImage = document . getElementById ( 'my-fabric-canvas' ) ,
15
+ dataURL = filteredImage . toDataURL ( ) ;
16
+ photoTakenEncoded ( dataURL ) ;
17
+ }
18
+
19
19
React . useEffect ( ( ) => {
20
- canvas = new fabric . Canvas ( "my-fabric-canvas" ) ;
21
- canvas . setDimensions ( {
22
- width : aspectRatio . width ,
23
- height : aspectRatio . height
24
- } ) ;
25
- console . log ( aspectRatio )
26
- } , [ ] ) ;
20
+ canvas = new fabric . Canvas ( "my-fabric-canvas" ) ;
21
+ canvas . setDimensions ( {
22
+ width : dimensions . width ,
23
+ height : dimensions . height
24
+ } ) ;
25
+ console . log ( dimensions )
26
+ } , [ canvas ] ) ;
27
27
28
28
29
- const addBackground = ( photoTaken ) =>
30
- fabric . Image . fromURL ( photoTaken , ( oImg ) =>
31
- canvas . setBackgroundImage ( oImg , canvas . renderAll . bind ( canvas ) , {
29
+ const addBackground = ( photoTaken ) =>
30
+ fabric . Image . fromURL ( photoTaken , ( oImg ) =>
31
+ canvas . setBackgroundImage ( oImg , canvas . renderAll . bind ( canvas ) , {
32
32
// Optionally add an opacity lvl to the image
33
33
backgroundImageOpacity : 0.5 ,
34
34
// should the image be resized to fit the container?
35
35
backgroundImageStretch : false ,
36
36
scaleX : canvas . width / oImg . width ,
37
37
scaleY : canvas . height / oImg . height
38
- } )
39
- ) ;
38
+ } )
39
+ ) ;
40
40
41
- addBackground ( photoTaken )
42
-
43
- // These two functions are equivalent, just sharing how to arrow-syntax for fun
44
- const decorateImage = ( url ) =>
45
- fabric . Image . fromURL ( url , ( oImg ) =>
46
- canvas . add ( oImg )
47
- ) ;
41
+ const removeItem = ( ) => {
42
+ let object = canvas . getActiveObject ( ) ;
43
+ if ( ! object ) {
44
+ alert ( 'Please select the element to remove' ) ;
45
+ return '' ;
46
+ }
47
+ canvas . remove ( object ) ;
48
+ }
49
+
50
+ addBackground ( photoTaken )
48
51
49
- // function decorateImage (url) {
50
- // fabric.Image.fromURL (url, function(oImg) {
51
- // canvas.add( oImg);
52
- // });
53
- // }
52
+ // These two functions are equivalent, just sharing how to arrow-syntax for fun
53
+ const decorateImage = ( url ) =>
54
+ fabric . Image . fromURL ( url , ( oImg ) =>
55
+ canvas . add ( oImg )
56
+ ) ;
54
57
55
58
return (
56
59
< animated . div style = { opacity } className = "decorate-page" >
@@ -81,7 +84,8 @@ const Decorate = ( {stage, setStage, photoTaken, photoTakenEncoded, aspectRatio,
81
84
type = "button"
82
85
className = "btn"
83
86
onClick = { ( ) => {
84
- setStage ( "share" )
87
+ setStage ( "share" )
88
+ updateState ( )
85
89
} } >
86
90
Let's Share!
87
91
</ button >
@@ -117,31 +121,39 @@ const Decorate = ( {stage, setStage, photoTaken, photoTakenEncoded, aspectRatio,
117
121
</ div >
118
122
< div className = "container-inner decoration-container" >
119
123
< p > Let's decorate:</ p >
120
- < img
121
- className = "decoration"
122
- src = "images/panda_ears.png"
123
- alt = "panda bear ears"
124
- onClick = { e => decorateImage ( e . target . src ) }
124
+ < button
125
+ type = "button"
126
+ className = "btn"
127
+ onClick = { ( ) => {
128
+ removeItem ( )
129
+ } } >
130
+ Remove Active Object
131
+ </ button >
132
+ < img
133
+ className = "decoration"
134
+ src = "images/panda_ears.png"
135
+ alt = "panda bear ears"
136
+ onClick = { e => decorateImage ( e . target . src ) }
125
137
> </ img >
126
- < img
127
- className = "decoration"
128
- src = "images/cat-ears.png"
129
- alt = "cat ears"
130
- onClick = { e => decorateImage ( e . target . src ) }
138
+ < img
139
+ className = "decoration"
140
+ src = "images/cat-ears.png"
141
+ alt = "cat ears"
142
+ onClick = { e => decorateImage ( e . target . src ) }
131
143
> </ img >
132
- < img
133
- className = "decoration"
134
- src = "images/sparkles.png"
135
- alt = "sparkles"
136
- onClick = { e => decorateImage ( e . target . src ) }
144
+ < img
145
+ className = "decoration"
146
+ src = "images/sparkles.png"
147
+ alt = "sparkles"
148
+ onClick = { e => decorateImage ( e . target . src ) }
137
149
> </ img >
138
- < img
139
- className = "decoration"
140
- src = "images/devil.png"
141
- alt = "devil horns"
142
- onClick = { e => decorateImage ( e . target . src ) }
150
+ < img
151
+ className = "decoration"
152
+ src = "images/devil.png"
153
+ alt = "devil horns"
154
+ onClick = { e => decorateImage ( e . target . src ) }
143
155
> </ img >
144
-
156
+
145
157
</ div >
146
158
< div className = "statusbar" >
147
159
< div className = "left" > 3 object(s)</ div >
0 commit comments