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 0000000..df48005 Binary files /dev/null and b/resources/BulletHole.png differ