Skip to content

Commit 549b362

Browse files
committed
Step Before Final Looks
1 parent a7ed392 commit 549b362

File tree

5 files changed

+41
-16
lines changed

5 files changed

+41
-16
lines changed

index.html

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
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>

src/App.vue

+9-4
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,33 @@
11
<template>
22
<div id="app">
33
<ColorPicker :color="color"/>
4-
<Canvas/>
4+
<Canvas :pixels="pixels"/>
55
</div>
66
</template>
77

88
<script>
99
import Canvas from "./components/Canvas";
1010
import ColorPicker from "./components/ColorPicker";
1111
12+
const defaultColor = "white";
13+
1214
export default {
1315
name: "App",
1416
data: function() {
1517
return {
16-
color: "white"
18+
color: defaultColor,
19+
pixels: Array(30 * 30)
20+
.fill()
21+
.map(() => defaultColor)
1722
};
1823
},
1924
components: {
2025
Canvas,
2126
ColorPicker
2227
},
2328
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);
2631
});
2732
}
2833
};

src/components/Canvas.vue

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<template>
22
<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"/>
44
</div>
55
</template>
66

@@ -11,6 +11,9 @@ export default {
1111
name: "Canvas",
1212
components: {
1313
Pixel
14+
},
15+
props: {
16+
pixels: Array
1417
}
1518
};
1619
</script>

src/components/ColorPicker.vue

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
<template>
22
<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"/>
77
</div>
88
</template>
99

src/components/Pixel.vue

+11-7
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
<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>
63
</template>
74

85
<script>
@@ -11,11 +8,18 @@ export default {
118
props: {
129
color: String,
1310
current: Boolean,
14-
interactive: Boolean
11+
isInColorPicker: Boolean,
12+
isInCanvas: Boolean,
13+
index: Number
1514
},
1615
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+
}
1923
}
2024
}
2125
};

0 commit comments

Comments
 (0)