File tree 5 files changed +41
-16
lines changed
5 files changed +41
-16
lines changed Original file line number Diff line number Diff line change
1
+ <!DOCTYPE html>
2
+ < html >
3
+ < head >
4
+ < meta charset ="utf-8 " />
5
+ < meta name ="viewport " content ="width=device-width,initial-scale=1.0 " />
6
+ < title > Pixel Drawing App - Made with Vue.js</ title >
7
+ </ head >
8
+
9
+ < body >
10
+ < div id ="app "> </ div >
11
+ <!-- built files will be auto injected -->
12
+ </ body >
13
+ </ html >
Original file line number Diff line number Diff line change 1
1
<template >
2
2
<div id =" app" >
3
3
<ColorPicker :color =" color" />
4
- <Canvas />
4
+ <Canvas :pixels = " pixels " />
5
5
</div >
6
6
</template >
7
7
8
8
<script >
9
9
import Canvas from " ./components/Canvas" ;
10
10
import ColorPicker from " ./components/ColorPicker" ;
11
11
12
+ const defaultColor = " white" ;
13
+
12
14
export default {
13
15
name: " App" ,
14
16
data : function () {
15
17
return {
16
- color: " white"
18
+ color: defaultColor,
19
+ pixels: Array (30 * 30 )
20
+ .fill ()
21
+ .map (() => defaultColor)
17
22
};
18
23
},
19
24
components: {
20
25
Canvas,
21
26
ColorPicker
22
27
},
23
28
mounted () {
24
- this .$root .$on (" updatecolor " , color => {
25
- this .color = color;
29
+ this .$root .$on (" clickedpixel " , index => {
30
+ this .pixels . splice (index, 1 , this . color ) ;
26
31
});
27
32
}
28
33
};
Original file line number Diff line number Diff line change 1
1
<template >
2
2
<div class =" canvas" >
3
- <Pixel v-for =" n in 30*30 " color =" blue " />
3
+ <Pixel isInCanvas v-for =" (color, index) in pixels " :key = " index " :index = " index " : color =" color " />
4
4
</div >
5
5
</template >
6
6
@@ -11,6 +11,9 @@ export default {
11
11
name: " Canvas" ,
12
12
components: {
13
13
Pixel
14
+ },
15
+ props: {
16
+ pixels: Array
14
17
}
15
18
};
16
19
</script >
Original file line number Diff line number Diff line change 1
1
<template >
2
2
<div >
3
- <Pixel interactive color =" white" :current =" color == 'white' ? true : false" />
4
- <Pixel interactive color =" lightblue" :current =" color == 'lightblue' ? true : false" />
5
- <Pixel interactive color =" blue" :current =" color == 'blue' ? true : false" />
6
- <Pixel interactive color =" darkblue" :current =" color == 'darkblue' ? true : false" />
3
+ <Pixel isInColorPicker color =" white" :current =" color == 'white' ? true : false" />
4
+ <Pixel isInColorPicker color =" lightblue" :current =" color == 'lightblue' ? true : false" />
5
+ <Pixel isInColorPicker color =" blue" :current =" color == 'blue' ? true : false" />
6
+ <Pixel isInColorPicker color =" darkblue" :current =" color == 'darkblue' ? true : false" />
7
7
</div >
8
8
</template >
9
9
Original file line number Diff line number Diff line change 1
1
<template >
2
- <div
3
- @click =" interactive && changeColor(color)"
4
- :class =" ['pixel', color, current ? 'current' : '']"
5
- ></div >
2
+ <div @click =" handleClick" :class =" ['pixel', color, current ? 'current' : '']" ></div >
6
3
</template >
7
4
8
5
<script >
@@ -11,11 +8,18 @@ export default {
11
8
props: {
12
9
color: String ,
13
10
current: Boolean ,
14
- interactive: Boolean
11
+ isInColorPicker: Boolean ,
12
+ isInCanvas: Boolean ,
13
+ index: Number
15
14
},
16
15
methods: {
17
- changeColor : function (color ) {
18
- this .$root .$emit (" updatecolor" , color);
16
+ handleClick : function () {
17
+ if (this .isInColorPicker ) {
18
+ this .$root .$emit (" updatecolor" , this .color );
19
+ }
20
+ if (this .isInCanvas ) {
21
+ this .$root .$emit (" clickedpixel" , this .index );
22
+ }
19
23
}
20
24
}
21
25
};
You can’t perform that action at this time.
0 commit comments