From 755c236d954d623de9c68b30c3c5d5facc1f5b2f Mon Sep 17 00:00:00 2001 From: CBenni Date: Sun, 7 Jan 2018 03:15:32 +0100 Subject: [PATCH] This is probably a bad idea. --- config.default.json | 22 ++++++++++++++++++++-- index.js | 24 ++++++++++++++++++++---- resources/BulletHole.png | Bin 0 -> 6303 bytes 3 files changed, 40 insertions(+), 6 deletions(-) create mode 100644 resources/BulletHole.png diff --git a/config.default.json b/config.default.json index 3a6451d..4669171 100644 --- a/config.default.json +++ b/config.default.json @@ -279,6 +279,24 @@ "size": 128 } } - }] - } + }], + "shoot": { + "src": "./resources/BulletHole.png", + "z": 2, + "anchor": { + "x": { + "absolute": true, + "position": 0, + "offset": "Math.random()*imgWidth", + "size": "123*5" + }, + "y": { + "absolute": true, + "position": 0, + "offset": "Math.random()*imgHeight", + "size": "130*5" + } + } + } + } } diff --git a/index.js b/index.js index bac1bdb..7b66670 100644 --- a/index.js +++ b/index.js @@ -34,9 +34,17 @@ _.each(templates, (template, templateName) => { // size = width or height of the template/image // anchor = the corresponding anchor config function calculatePosition(scale, anchor, imageSize) { + if (anchor.absolute) { + return anchor.offset; + } return imageSize * anchor.position / 100 - anchor.offset * scale; } +function getNumericAnchor(anchor, imgWidth, imgHeight) { // eslint-disable-line no-unused-vars + return _.mapValues(anchor, dimension => + _.mapValues(dimension, value => (Number.isFinite(value) ? Number(value) : eval(value)))); // eslint-disable-line no-eval +} + function render(template, img, size, flipH) { let imgWidth = img.width; let imgHeight = img.height; @@ -49,13 +57,21 @@ function render(template, img, size, flipH) { if (!size.height) imgHeight = imgHeight * size.width / img.width; } - const xScale = imgWidth / template.anchor.x.size; - const yScale = imgHeight / template.anchor.y.size; + const anchor = getNumericAnchor(template.anchor, imgWidth, imgHeight); + console.log('Numeric anchor: ', anchor); + const xScale = imgWidth / anchor.x.size; + const yScale = imgHeight / anchor.y.size; const templateScale = Math.max(0, Math.min(10, Math.max(xScale || 0, yScale || 0))); + let templateOffsetX; + let templateOffsetY; + templateOffsetX = calculatePosition(templateScale, anchor.x, imgWidth); + templateOffsetY = calculatePosition(templateScale, anchor.y, imgHeight); - let templateOffsetX = calculatePosition(templateScale, template.anchor.x, imgWidth); - let templateOffsetY = calculatePosition(templateScale, template.anchor.y, imgHeight); + console.log('xScale', xScale); + console.log('yScale', yScale); + console.log('templateOffsetX', templateOffsetX); + console.log('templateOffsetY', templateOffsetY); let imageOffsetX = 0; let imageOffsetY = 0; diff --git a/resources/BulletHole.png b/resources/BulletHole.png new file mode 100644 index 0000000000000000000000000000000000000000..df480050ea9f942aa25376898bf2a5f04e99521b GIT binary patch literal 6303 zcmV;Q7+~j#P)+`Ll9-*9 znVp7-j0Xq^o1deSmz$ubt(TXV0s;h_p{J3SoCpgJj+2^reSwCHkh#6Ufrg8mpsNK1 z1_J~He}jmhqpYQ>vzVW%kd&RBqNbLcpQEU;ousa_x4W2}pq`(l4G$8juCk7mou#a? zp{B0{2MUvzo}8hjkd>RTv$(CXwTh0C-{0Td-Qt#;p`N0vmz<)nw7a6BseOWmr>m}! zk&@EY*#ZOslA5EGnxBi2nQn7;U14RNourhMnuLp!dw+!2*V*9VGT zk(k@t+l`Hnnw_i}93Cw&G?9^(!o$ORe149Sn_*>XMoCN_A|}bq(2b6iwY9pQo}SFm z)v~s}proj$sH=*Kj+vmQoTR6qp`zd8=;Y_^o1dysR9LvZ#Y|6A)z#L8g@rvpM1X*R zp`oQFC@ROx)a2ynuCK3_n4s+L@SdTln4PJdpQfa!t%b!A%>V!Z0d!JMQvg8b*k%9# z00Cl4M??UK1szBL000SaNLh0L01FcU01FcV0GgZ_00007bV*G`2jT$-0t+bODx`k^ z02LcaL_t(|+U=Y9SJL_S$D4nkIcNH%IWs?f=ggToL69S;C^BStlz>EKQ78gL1;tE6 zG`AELmsCV^UnX}=bGI=pbGOmTvTrq6nH6o*G}W}sk{{3a^?n0pAc%c)rt>}D=TQ(8 z-Vg4*uY2!nal?lH*Z&1Q`WPT8KL&_v9|Odxk0B(Ue*_ShE`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