foxy-moxy/js/fox.js

106 lines
2.9 KiB
JavaScript
Raw Normal View History

2016-12-15 20:06:02 +00:00
// TODO: use query params for these
2016-12-15 22:21:30 +00:00
var Chance = require('chance');
var chance = new Chance();
2016-12-15 20:06:02 +00:00
2016-12-15 23:21:12 +00:00
var hsl = function (h, s, l) {
return "hsl(" + h + "," + s + "%, " + l + "%)";
}
2016-12-15 21:23:38 +00:00
var Fox = function (IMG_WIDTH, IMG_HEIGHT) {
2016-12-15 20:06:02 +00:00
// head top left corner
2016-12-15 21:23:38 +00:00
var origin = {x: IMG_WIDTH / 4, y: IMG_HEIGHT / 4};
// TODO: head headWidth and height
var headWidth = IMG_WIDTH / 2;
var headHeight = IMG_HEIGHT / 2;
2016-12-15 23:21:12 +00:00
var kappa = chance.floating({min: 0.2, max: 0.45})
var hue = chance.integer({min: 5, max: 50});
2016-12-15 23:21:12 +00:00
var saturation = chance.integer({min: 70, max: 90});
var lightness = chance.integer({min: 40, max: 60});
var headColor = hsl(hue, saturation, lightness);
2016-12-15 20:06:02 +00:00
var ears = (function () {
var offsetX = chance.floating({min: 0.17 * headWidth, max: 0.2 * headWidth});
var angle = chance.floating({min: 0.05 * Math.PI, max: 0.2 * Math.PI});
2016-12-15 22:42:11 +00:00
// TODO: size
2016-12-15 20:06:02 +00:00
return {
2016-12-15 23:21:12 +00:00
color: headColor,
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
}
};
}());
var eyes = (function () {
2016-12-15 22:42:11 +00:00
// TODO: color
2016-12-16 02:17:29 +00:00
var offsetY = chance.floating({min: -0.05 * headHeight, max: -0.025 * headHeight});
2016-12-15 23:21:12 +00:00
var offsetX = chance.floating({min: 0.13 * headWidth, max: 0.25 * headWidth});
2016-12-15 22:42:11 +00:00
var eyeHeight = chance.floating({min: 0.08 * headHeight, max: 0.13 * headHeight});
2016-12-15 22:42:11 +00:00
return {
height: eyeHeight,
width: eyeHeight/2,
2016-12-16 02:17:29 +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-15 20:06:02 +00:00
2016-12-15 23:21:12 +00:00
var nose = (function (eyes) {
return {
x: origin.x + (headWidth/2),
y: (eyes.left.y + 0.3 * (origin.y + headHeight - eyes.left.y)),
2016-12-16 02:17:29 +00:00
width: 0.04 * headWidth,
height: 0.03 * headWidth
2016-12-15 23:21:12 +00:00
}
}(eyes));
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(
chance.integer({min:0, max:360}),
chance.integer({min:0, max:100}),
chance.integer({min:10, max:100})
2016-12-16 00:30:59 +00:00
)
2016-12-15 21:23:38 +00:00
},
head: {
origin: origin,
2016-12-15 21:39:02 +00:00
width: headWidth,
2016-12-15 23:21:12 +00:00
height: headHeight,
color: headColor,
kappa: kappa,
maskColor: hsl(hue, saturation, 95),
maskWidth: chance.integer({min: 0.5 * IMG_WIDTH, max: IMG_WIDTH}),
2016-12-16 02:17:29 +00:00
maskHeight: chance.integer({min: 1.7 * (IMG_HEIGHT - eyes.left.y), max: 1.85 * (IMG_HEIGHT - eyes.left.y)})
},
2016-12-15 20:06:02 +00:00
ears: ears,
eyes: eyes,
2016-12-15 23:21:12 +00:00
nose: nose,
2016-12-15 21:23:38 +00:00
// mouth: mouth
2016-12-15 20:06:02 +00:00
};
2016-12-15 21:23:38 +00:00
};
module.exports = Fox;