2016-12-22 19:40:48 +00:00
|
|
|
const Chance = require('chance');
|
2016-12-15 20:06:02 +00:00
|
|
|
|
2016-12-22 19:40:48 +00:00
|
|
|
const hsl = function (h, s, l) {
|
2016-12-15 23:21:12 +00:00
|
|
|
return "hsl(" + h + "," + s + "%, " + l + "%)";
|
|
|
|
}
|
|
|
|
|
2016-12-22 19:40:48 +00:00
|
|
|
const Fox = function (IMG_WIDTH, IMG_HEIGHT, seed) {
|
2016-12-16 04:20:51 +00:00
|
|
|
if (seed) {
|
|
|
|
chance = new Chance(seed);
|
|
|
|
} else {
|
|
|
|
chance = new Chance();
|
|
|
|
}
|
2016-12-15 20:06:02 +00:00
|
|
|
|
2016-12-19 23:30:43 +00:00
|
|
|
// origin: head top left corner
|
2016-12-22 19:40:48 +00:00
|
|
|
const kappa = chance.floating({min: 0.2, max: 0.45})
|
2016-12-15 23:21:12 +00:00
|
|
|
|
2016-12-22 19:40:48 +00:00
|
|
|
const hue = chance.integer({min: 5, max: 50});
|
|
|
|
const saturation = chance.integer({min: 70, max: 90});
|
|
|
|
const lightness = chance.integer({min: 40, max: 60});
|
2016-12-15 20:06:02 +00:00
|
|
|
|
2016-12-22 19:40:48 +00:00
|
|
|
const head = {
|
2016-12-19 23:30:43 +00:00
|
|
|
width: 0.6 * IMG_WIDTH,
|
|
|
|
height: 0.6 * IMG_HEIGHT,
|
|
|
|
kappa: kappa,
|
|
|
|
color: hsl(hue, saturation, lightness)
|
|
|
|
}
|
|
|
|
|
2016-12-22 19:40:48 +00:00
|
|
|
const origin = {x: IMG_WIDTH / 2 - head.width / 2, y: 0.5 * IMG_HEIGHT - head.height / 2};
|
2016-12-19 23:30:43 +00:00
|
|
|
|
2016-12-22 19:40:48 +00:00
|
|
|
const ears = (function (origin, headWidth, headHeight, headColor) {
|
|
|
|
const offsetX = chance.floating({min: 0.17 * headWidth, max: 0.2 * headWidth});
|
|
|
|
const angle = chance.floating({min: 0.05 * Math.PI, max: 0.2 * Math.PI});
|
2016-12-15 20:06:02 +00:00
|
|
|
return {
|
2016-12-15 23:21:12 +00:00
|
|
|
color: headColor,
|
2016-12-16 01:14:03 +00:00
|
|
|
kappa: 0.9 * kappa,
|
2016-12-15 20:06:02 +00:00
|
|
|
left: {
|
2016-12-15 21:23:38 +00:00
|
|
|
x: origin.x + (headWidth/2) - offsetX,
|
2016-12-15 22:21:30 +00:00
|
|
|
y: origin.y + (0.15 * headHeight),
|
|
|
|
angle: angle,
|
|
|
|
width: 0.4 * headWidth,
|
2016-12-16 02:17:29 +00:00
|
|
|
height: 0.8 * headHeight
|
2016-12-15 20:06:02 +00:00
|
|
|
},
|
|
|
|
right: {
|
2016-12-15 21:23:38 +00:00
|
|
|
x: origin.x + (headWidth/2) + offsetX,
|
2016-12-15 22:21:30 +00:00
|
|
|
y: origin.y + (0.15 * headHeight),
|
|
|
|
angle: -angle,
|
|
|
|
width: 0.4 * headWidth,
|
2016-12-16 02:17:29 +00:00
|
|
|
height: 0.8 * headHeight
|
2016-12-15 20:06:02 +00:00
|
|
|
}
|
|
|
|
};
|
2016-12-19 23:30:43 +00:00
|
|
|
}(origin, head.width, head.height, head.color));
|
2016-12-15 20:21:43 +00:00
|
|
|
|
2016-12-22 19:40:48 +00:00
|
|
|
const eyes = (function (origin, headWidth, headHeight) {
|
2016-12-15 22:42:11 +00:00
|
|
|
// TODO: color
|
2016-12-22 19:40:48 +00:00
|
|
|
const offsetY = chance.floating({min: -0.05 * headHeight, max: -0.025 * headHeight});
|
|
|
|
const offsetX = chance.floating({min: 0.13 * headWidth, max: 0.25 * headWidth});
|
2016-12-15 22:42:11 +00:00
|
|
|
|
2016-12-22 19:40:48 +00:00
|
|
|
const eyeHeight = chance.floating({min: 0.08 * headHeight, max: 0.13 * headHeight});
|
2016-12-16 01:14:03 +00:00
|
|
|
|
2016-12-15 22:42:11 +00:00
|
|
|
return {
|
2016-12-16 01:14:03 +00:00
|
|
|
height: eyeHeight,
|
|
|
|
width: eyeHeight/2,
|
2016-12-16 02:34:42 +00:00
|
|
|
style: 'ellipse',
|
2016-12-19 23:39:09 +00:00
|
|
|
// style: chance.pickone(['ellipse', 'smiley']),
|
2016-12-15 22:42:11 +00:00
|
|
|
left: {
|
|
|
|
x: origin.x + (headWidth/2) - offsetX,
|
|
|
|
y: origin.y + (headHeight/2) + offsetY
|
|
|
|
},
|
|
|
|
right: {
|
|
|
|
x: origin.x + (headWidth/2) + offsetX,
|
|
|
|
y: origin.y + (headHeight/2) + offsetY
|
|
|
|
}
|
|
|
|
}
|
2016-12-19 23:30:43 +00:00
|
|
|
}(origin, head.width, head.height));
|
2016-12-15 20:06:02 +00:00
|
|
|
|
2016-12-22 19:40:48 +00:00
|
|
|
const nose = {
|
2016-12-19 23:30:43 +00:00
|
|
|
x: origin.x + (head.width/2),
|
|
|
|
y: (eyes.left.y + chance.floating({min: 0.2, max: 0.4}) * (origin.y + head.height - eyes.left.y)),
|
|
|
|
width: chance.floating({min: 0.03, max: 0.04}) * head.width,
|
|
|
|
height: chance.floating({min: 0.03, max: 0.04}) * head.width
|
|
|
|
}
|
2016-12-15 23:21:12 +00:00
|
|
|
|
2016-12-22 19:40:48 +00:00
|
|
|
const mouth = {
|
2016-12-19 23:30:43 +00:00
|
|
|
x: origin.x + (head.width/2),
|
|
|
|
y: (nose.y + chance.floating({min: 0.2, max: 0.35}) * (origin.y + head.height - nose.y)),
|
|
|
|
width: chance.floating({min: 0.08, max: 0.15}) * head.width,
|
|
|
|
height: chance.floating({min: 0.03, max: 0.06}) * head.width,
|
|
|
|
style: chance.pickone(['smirk', 'cat', 'none'])
|
|
|
|
}
|
|
|
|
|
2016-12-22 19:40:48 +00:00
|
|
|
const mask = {
|
2016-12-19 23:30:43 +00:00
|
|
|
width: chance.floating({min: 0.5 * IMG_WIDTH, max: IMG_WIDTH}),
|
|
|
|
height: chance.floating({min: 1.7 * (IMG_HEIGHT - eyes.left.y), max: 1.85 * (IMG_HEIGHT - eyes.left.y)})
|
|
|
|
}
|
|
|
|
head.mask = mask;
|
2016-12-16 02:34:42 +00:00
|
|
|
|
2016-12-15 20:06:02 +00:00
|
|
|
return {
|
2016-12-15 21:23:38 +00:00
|
|
|
canvas: {
|
|
|
|
height: IMG_HEIGHT,
|
2016-12-16 00:30:59 +00:00
|
|
|
width: IMG_WIDTH,
|
|
|
|
color: hsl(
|
2016-12-16 01:14:03 +00:00
|
|
|
chance.integer({min:0, max:360}),
|
|
|
|
chance.integer({min:0, max:100}),
|
|
|
|
chance.integer({min:10, max:100})
|
2016-12-19 23:30:43 +00:00
|
|
|
),
|
2016-12-15 20:21:43 +00:00
|
|
|
},
|
2016-12-19 23:30:43 +00:00
|
|
|
head: head,
|
2016-12-15 20:06:02 +00:00
|
|
|
ears: ears,
|
|
|
|
eyes: eyes,
|
2016-12-15 23:21:12 +00:00
|
|
|
nose: nose,
|
2016-12-16 02:34:42 +00:00
|
|
|
mouth: mouth
|
2016-12-15 20:06:02 +00:00
|
|
|
};
|
2016-12-15 21:23:38 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = Fox;
|