It has been a long time sense I wrote a post on vuejs, so I thought I would make a vuejs example post to help expand that collection. For this vuejs example the idea of a simple image editor application came to mind as just one of many ideas that might prove to be fun. So maybe something like that is in order when it comes to expanding on what can be done with vuejs. After all once I cover all the basics the only thing to do from that point forward is to start to create some actual projects one one type or another.
The general idea I have for this image editor application is to not make any kind of major project out of it that will be a full blown image manipulation program. All I want to do is just have a way to make my own image asset standard, and have a simple little tool that can be used to create and edit such a standard.
1 - The main.js file for this Vuejs powered image editor
First off here is the main vuejs instance that will make use of some components that I worked out for this example. For the main vue instance I went with a simple static template rather than a render function. However in the template I am using some components that are making use of render functions when it comes to drawing the current state of the image grid.
// set the current image pix pos to the current image color index
pSet: function(x, y){
var dat = this.$data;
var img = dat.imgs[dat.currentImage];
var pxIndex = y * img.width + Number(x);
img.data[pxIndex] = Number(img.colorIndex);
// force update all children
this.$children.forEach(function(child){
if(child.updateText){
child.updateText();
}
});
},
pxClickHandler: function(x, y){
this.pSet(x, y);
},
colorClickHandler: function(index){
var dat = this.$data;
var img = dat.imgs[dat.currentImage];
img.colorIndex = index;
},
load: function(json){
var pixmapObj = JSON.parse(json);
var imgs = IMG.createIMGSFromPixmap(pixmapObj);
//console.log(imgs);
this.$data.imgs = Vue.observable(imgs);
//this.$data.imgs = imgs;
}
}
});
2 - The grid div component
I wanted to break this project down into a few components as a way to make things a little more fine grain. If I keep working on this I will most likely end up with a lot of components actually, but one that I am really going to want for this image editor is a components that will draw the current state of the current image.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
Vue.component('image-div-grid', {
props: ['img'],
render: function(createElement){
var img = this.$props.img,
vm = this,
divs = [];
img.data.forEach(function(px, i){
var x = i % img.width,
y = Math.floor(i / img.width),
color = img.palette[px];
var px = createElement('div', {
attrs: {
id: 'px_' + x + '_' + y,
class: 'image-div-grid-px',
style: 'left:' + ( x * img.pxSize ) + 'px;top:'+ ( y * img.pxSize ) +';background:' + color + ';'
I will want to have another component that will be used to set the current color to draw with when I click a div element in the image div grid component.
var str = 'left:' + ( x * 32 ) + 'px;top:'+ ( y * 32 ) +';background:' + color + ';';
if(Number(img.colorIndex) === Number(i)){
str += 'outline:3px solid #afafaf;z-index:1;'
}else{
str += 'z-index:0;'
}
return str;
},
colorClick: function(e){
var div = e.target,
idArr = div.id.split('_')
this.$emit('color-click', idArr[1]);
}
}
});
3 - The json component
I will then want an additional component that I can use to save and load the state of an animation. There are a number of things that I can do when it comes to this, but for now I just want a text area element that I can use to copy and paste the JSON to and from.
background-position:00, 12.5px0, 12.5px -12.5px, 0px12.5px; /* Must be half of one side of the square */
}
.image-div-grid-px{
position:absolute;
width:32px;
height:32px;
}
.image-color-pick{
}
.image-color-pick-div{
position:absolute;
width:32px;
height:32px;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<html>
<head>
<title>vue example</title>
<scriptsrc="/js/vuejs/2.6.10/vue.js"></script>
<linkrel="stylesheet"href="style.css">
</head>
<body>
<divid="app"></div>
<scriptsrc="lib/img.js"></script>
<scriptsrc="comp/image-div-grid.js"></script>
<scriptsrc="comp/image-color-pick.js"></script>
<scriptsrc="comp/image-text-pixmap.js"></script>
<scriptsrc="main.js"></script>
</body>
</html>
5 - Conclusion
I did not get around to finishing everything that i wanted to do with this vuejs example as of this writing. I am not happy with the way things pan out, and will get around to writing the example over again at some point when I get more time to work on this one. I have been getting around to editing some of this vuejs content of mine, and if I keep up with it I should come back to this one again at some point in the future.