This is probably a bad idea.

This commit is contained in:
CBenni 2018-01-07 03:15:32 +01:00
parent 1fcd8dd5e1
commit 755c236d95
3 changed files with 40 additions and 6 deletions

View file

@ -279,6 +279,24 @@
"size": 128 "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"
}
}
}
} }
} }

View file

@ -34,9 +34,17 @@ _.each(templates, (template, templateName) => {
// size = width or height of the template/image // size = width or height of the template/image
// anchor = the corresponding anchor config // anchor = the corresponding anchor config
function calculatePosition(scale, anchor, imageSize) { function calculatePosition(scale, anchor, imageSize) {
if (anchor.absolute) {
return anchor.offset;
}
return imageSize * anchor.position / 100 - anchor.offset * scale; 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) { function render(template, img, size, flipH) {
let imgWidth = img.width; let imgWidth = img.width;
let imgHeight = img.height; let imgHeight = img.height;
@ -49,13 +57,21 @@ function render(template, img, size, flipH) {
if (!size.height) imgHeight = imgHeight * size.width / img.width; if (!size.height) imgHeight = imgHeight * size.width / img.width;
} }
const xScale = imgWidth / template.anchor.x.size; const anchor = getNumericAnchor(template.anchor, imgWidth, imgHeight);
const yScale = imgHeight / template.anchor.y.size; 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))); 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); console.log('xScale', xScale);
let templateOffsetY = calculatePosition(templateScale, template.anchor.y, imgHeight); console.log('yScale', yScale);
console.log('templateOffsetX', templateOffsetX);
console.log('templateOffsetY', templateOffsetY);
let imageOffsetX = 0; let imageOffsetX = 0;
let imageOffsetY = 0; let imageOffsetY = 0;

BIN
resources/BulletHole.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.2 KiB