Skip to content

Commit 1a852cc

Browse files
author
Sam Goto
committed
starts generalizing asset placement
1 parent d8f5b02 commit 1a852cc

File tree

4 files changed

+126
-111
lines changed

4 files changed

+126
-111
lines changed

docs/bundle.js

+53-49
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const HtmlDom = require('htmldom');
33
const RssParser = require('rss-parser');
44

55
class Document {
6-
constructor(markup) {
6+
constructor() {
77
this.dom = new HtmlDom(`
88
<person>
99
<shoulder right />
@@ -28,40 +28,6 @@ class Document {
2828
`);
2929
}
3030

31-
load(model) {
32-
}
33-
34-
static xml(node, level) {
35-
let result = [];
36-
let attributes = "";
37-
if (node.attributes) {
38-
attributes = " " + Object.entries(node.attributes).map(([key, value]) => {
39-
if (value == null) {
40-
return `${key}`
41-
}
42-
return `${key}=${value}`
43-
}).join(" ");
44-
}
45-
46-
if (node.type == "tag") {
47-
result.push(`${" ".repeat(level)}<${node.name}${attributes}>`);
48-
}
49-
50-
if (node.asset) {
51-
result.push(`${" ".repeat(level)}[${node.asset.value}]`);
52-
}
53-
54-
for (let child of node.children || []) {
55-
if (child.type == "tag") {
56-
result.push(Document.xml(child, level + 1));
57-
}
58-
}
59-
if (node.type == "tag") {
60-
result.push(`${" ".repeat(level)}</${node.name}>`);
61-
}
62-
return result.join("\n");
63-
}
64-
6531
apply(code) {
6632
let doc = this;
6733
let assets = code.$("asset");
@@ -94,18 +60,23 @@ class Document {
9460
let match = matches[i];
9561
// console.log(match);
9662
let resource = {};
97-
if (asset.attributes.type) {
98-
resource.type = asset.attributes.type;
99-
}
100-
if (asset.attributes.src) {
101-
resource.src = asset.attributes.src;
102-
}
103-
if (asset.children &&
104-
asset.children.length > 0 &&
105-
asset.children[0].value) {
106-
resource.value = asset.children[0].value.trim()
107-
}
108-
match.asset = resource;
63+
64+
// console.log(asset);
65+
asset.parent = match;
66+
match.children.push(asset);
67+
68+
//if (asset.attributes.type) {
69+
// resource.type = asset.attributes.type;
70+
//}
71+
//if (asset.attributes.src) {
72+
// resource.src = asset.attributes.src;
73+
//}
74+
//if (asset.children &&
75+
// asset.children.length > 0 &&
76+
// asset.children[0].value) {
77+
// resource.value = asset.children[0].value.trim()
78+
//}
79+
// match.asset = resource;
10980
// }
11081
}
11182
}
@@ -160,7 +131,7 @@ class Document {
160131
return this.dom.$(selector)[0];
161132
}
162133

163-
position(body, face) {
134+
load(body, face) {
164135
if (!body || !face) {
165136
return null;
166137
}
@@ -211,8 +182,41 @@ class Document {
211182
return this;
212183
}
213184

185+
static xml(node, level) {
186+
// console.log(node);
187+
188+
let result = [];
189+
let attributes = "";
190+
if (node.attributes) {
191+
attributes = " " + Object.entries(node.attributes).map(([key, value]) => {
192+
if (value == null) {
193+
return `${key}`
194+
}
195+
return `${key}=${value}`
196+
}).join(" ");
197+
}
198+
199+
if (node.type == "tag") {
200+
result.push(`${" ".repeat(level)}<${node.name}${attributes}>`);
201+
}
202+
203+
if (node.asset) {
204+
result.push(`${" ".repeat(level)}[${node.asset.value}]`);
205+
}
206+
207+
for (let child of node.children || []) {
208+
if (child.type == "tag") {
209+
result.push(Document.xml(child, level + 1));
210+
}
211+
}
212+
if (node.type == "tag") {
213+
result.push(`${" ".repeat(level)}</${node.name}>`);
214+
}
215+
return result.join("\n");
216+
}
217+
214218
toString() {
215-
return xml(this.dom.dom, 0);
219+
return Document.xml(this.dom.dom, 0);
216220
}
217221
}
218222

docs/polyfill.js

+11-7
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ async function main(feed) {
5454
// load the posenet model
5555
// let network = undefined;
5656
posenet.load().then((net) => {
57-
loop2(net);
57+
estimate(net);
5858
});
5959

6060
var ctracker = new clm.tracker();
@@ -67,18 +67,21 @@ async function main(feed) {
6767

6868
function loop() {
6969
requestAnimationFrame(loop);
70-
paint(doc.position(pose.keypoints,
71-
ctracker.getCurrentPosition()));
70+
if (!doc.load(pose.keypoints,
71+
ctracker.getCurrentPosition())) {
72+
return;
73+
}
74+
paint(doc);
7275
}
7376

74-
function loop2(network) {
77+
function estimate(network) {
7578
const imageScaleFactor = 0.2;
7679
const flipHorizontal = false;
7780
const outputStride = 16;
7881
network.estimateSinglePose(camera, imageScaleFactor, flipHorizontal, outputStride).then((data) => {
7982
pose = data;
8083
});
81-
setTimeout(loop2.bind(this, network), 1000);
84+
setTimeout(estimate.bind(this, network), 1000);
8285
}
8386

8487
function begin() {
@@ -124,6 +127,7 @@ async function main(feed) {
124127
let image = new Image();
125128
// image.style = "width: 100px, height: 100px";
126129
// image.width = "100px";
130+
// context.drawImage(image, nose.x - 25, nose.y - 25 * image.height / image.width, 50, 50 * image.height / image.width);
127131
image.src = "http://www.stickpng.com/assets/images/580b57fbd9996e24bc43bed5.png";
128132

129133
function paint(doc) {
@@ -138,6 +142,8 @@ async function main(feed) {
138142
context.lineWidth = 0.2;
139143
context.setLineDash([1, 2]);
140144

145+
// TODO(goto): generalize this to make it person-agnostic.
146+
141147
let person = doc.querySelector("person");
142148
marker(person);
143149
marker(doc.querySelector("face"), doc.querySelector("person"));
@@ -150,8 +156,6 @@ async function main(feed) {
150156
marker(doc.querySelector("eye[left]"), face);
151157
marker(doc.querySelector("eye[right]"), face);
152158

153-
// context.drawImage(image, nose.x - 25, nose.y - 25 * image.height / image.width, 50, 50 * image.height / image.width);
154-
155159
marker(doc.querySelector("nose"), face);
156160

157161
marker(doc.querySelector("mouth"), face);

index.js

+53-49
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ const HtmlDom = require('htmldom');
22
const RssParser = require('rss-parser');
33

44
class Document {
5-
constructor(markup) {
5+
constructor() {
66
this.dom = new HtmlDom(`
77
<person>
88
<shoulder right />
@@ -27,40 +27,6 @@ class Document {
2727
`);
2828
}
2929

30-
load(model) {
31-
}
32-
33-
static xml(node, level) {
34-
let result = [];
35-
let attributes = "";
36-
if (node.attributes) {
37-
attributes = " " + Object.entries(node.attributes).map(([key, value]) => {
38-
if (value == null) {
39-
return `${key}`
40-
}
41-
return `${key}=${value}`
42-
}).join(" ");
43-
}
44-
45-
if (node.type == "tag") {
46-
result.push(`${" ".repeat(level)}<${node.name}${attributes}>`);
47-
}
48-
49-
if (node.asset) {
50-
result.push(`${" ".repeat(level)}[${node.asset.value}]`);
51-
}
52-
53-
for (let child of node.children || []) {
54-
if (child.type == "tag") {
55-
result.push(Document.xml(child, level + 1));
56-
}
57-
}
58-
if (node.type == "tag") {
59-
result.push(`${" ".repeat(level)}</${node.name}>`);
60-
}
61-
return result.join("\n");
62-
}
63-
6430
apply(code) {
6531
let doc = this;
6632
let assets = code.$("asset");
@@ -93,18 +59,23 @@ class Document {
9359
let match = matches[i];
9460
// console.log(match);
9561
let resource = {};
96-
if (asset.attributes.type) {
97-
resource.type = asset.attributes.type;
98-
}
99-
if (asset.attributes.src) {
100-
resource.src = asset.attributes.src;
101-
}
102-
if (asset.children &&
103-
asset.children.length > 0 &&
104-
asset.children[0].value) {
105-
resource.value = asset.children[0].value.trim()
106-
}
107-
match.asset = resource;
62+
63+
// console.log(asset);
64+
asset.parent = match;
65+
match.children.push(asset);
66+
67+
//if (asset.attributes.type) {
68+
// resource.type = asset.attributes.type;
69+
//}
70+
//if (asset.attributes.src) {
71+
// resource.src = asset.attributes.src;
72+
//}
73+
//if (asset.children &&
74+
// asset.children.length > 0 &&
75+
// asset.children[0].value) {
76+
// resource.value = asset.children[0].value.trim()
77+
//}
78+
// match.asset = resource;
10879
// }
10980
}
11081
}
@@ -159,7 +130,7 @@ class Document {
159130
return this.dom.$(selector)[0];
160131
}
161132

162-
position(body, face) {
133+
load(body, face) {
163134
if (!body || !face) {
164135
return null;
165136
}
@@ -210,8 +181,41 @@ class Document {
210181
return this;
211182
}
212183

184+
static xml(node, level) {
185+
// console.log(node);
186+
187+
let result = [];
188+
let attributes = "";
189+
if (node.attributes) {
190+
attributes = " " + Object.entries(node.attributes).map(([key, value]) => {
191+
if (value == null) {
192+
return `${key}`
193+
}
194+
return `${key}=${value}`
195+
}).join(" ");
196+
}
197+
198+
if (node.type == "tag") {
199+
result.push(`${" ".repeat(level)}<${node.name}${attributes}>`);
200+
}
201+
202+
if (node.asset) {
203+
result.push(`${" ".repeat(level)}[${node.asset.value}]`);
204+
}
205+
206+
for (let child of node.children || []) {
207+
if (child.type == "tag") {
208+
result.push(Document.xml(child, level + 1));
209+
}
210+
}
211+
if (node.type == "tag") {
212+
result.push(`${" ".repeat(level)}</${node.name}>`);
213+
}
214+
return result.join("\n");
215+
}
216+
213217
toString() {
214-
return xml(this.dom.dom, 0);
218+
return Document.xml(this.dom.dom, 0);
215219
}
216220
}
217221

test/index.js

+9-6
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ describe("Doc tests", function(done) {
3434
});
3535
});
3636

37-
it("Object Model", function() {
37+
it.only("Object Model", function() {
3838
let doc = new Document();
3939
let code = new Selector(`
4040
<person>
@@ -46,10 +46,13 @@ describe("Doc tests", function(done) {
4646

4747
code.apply(doc);
4848

49-
assertThat(doc.querySelector("eye[right]").asset).equals({
50-
type: "image/png",
51-
src: "nose.png",
52-
});
49+
assertThat(doc.querySelector("eye[right]").children[0].name)
50+
.equals("asset");
51+
assertThat(doc.querySelector("eye[right]").children[0].attributes)
52+
.equals({
53+
type: "image/png",
54+
src: "nose.png",
55+
});
5356
});
5457

5558
it("Nose", function() {
@@ -115,7 +118,7 @@ describe("Doc tests", function(done) {
115118
face[7] = f(7);
116119

117120
// Applies the position.
118-
doc.position(body, face);
121+
doc.load(body, face);
119122

120123
// Asserts that the position were set correctly.
121124
assertThat(doc.querySelector("shoulder[right]")).isAt(6, 6, 6);

0 commit comments

Comments
 (0)