Added turtles
This commit is contained in:
parent
76a9510a7f
commit
4bb88b8296
2 changed files with 86 additions and 66 deletions
BIN
TurtleTemplate.png
Normal file
BIN
TurtleTemplate.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 46 KiB |
64
index.js
64
index.js
|
@ -12,11 +12,29 @@ var config = require("./config");
|
|||
|
||||
var cache = {};
|
||||
|
||||
const beeTemplate = new Image();
|
||||
beeTemplate.src = "./BeeTemplate.png";
|
||||
templates = {
|
||||
bee: {
|
||||
template: "./BeeTemplate.png",
|
||||
leftOffset: 475,
|
||||
widthTarget: 600,
|
||||
bottomTarget: 530
|
||||
},
|
||||
turtle: {
|
||||
template: "./TurtleTemplate.png",
|
||||
leftOffset: 210,
|
||||
widthTarget: 150,
|
||||
bottomTarget: 150
|
||||
}
|
||||
}
|
||||
|
||||
for(templateName in templates) {
|
||||
const data = templates[templateName];
|
||||
data.image = new Image();
|
||||
data.image.src = data.template;
|
||||
}
|
||||
|
||||
|
||||
function bee(url, size) {
|
||||
function render(template, url, size) {
|
||||
return new Promise((resolve, reject) => {
|
||||
console.log("Getting " + url);
|
||||
if (url) {
|
||||
|
@ -39,17 +57,13 @@ function bee(url, size) {
|
|||
if (!size.height) height = height * size.width / img.width;
|
||||
}
|
||||
|
||||
const leftOffset = 475.0; // relative to bee template width
|
||||
const widthTarget = 600.0; // relative to bee template width
|
||||
const bottomTarget = 530; // relative to bee template height
|
||||
|
||||
const beeScale = width / widthTarget; // scale the bee to fit the image
|
||||
const templateScale = width / template.widthTarget; // scale the template to fit the image
|
||||
|
||||
|
||||
const resultingWidth = beeTemplate.width * beeScale;
|
||||
let resultingHeight = beeTemplate.height * beeScale;
|
||||
let resultingWidth = template.image.width * templateScale;
|
||||
let resultingHeight = template.image.height * templateScale;
|
||||
|
||||
let imgTop = bottomTarget * beeScale - height; // naive top center position
|
||||
let imgTop = template.bottomTarget * templateScale - height; // naive top center position
|
||||
|
||||
if (imgTop < 0) {
|
||||
resultingHeight -= imgTop;
|
||||
|
@ -57,21 +71,25 @@ function bee(url, size) {
|
|||
}
|
||||
|
||||
const resultingImgTop = imgTop;
|
||||
const resultingImgLeft = leftOffset * beeScale - width / 2.0;
|
||||
const resultingImgLeft = template.leftOffset * templateScale - width / 2.0;
|
||||
|
||||
const resultingBeeTop = resultingHeight - beeTemplate.height * beeScale;
|
||||
const resultingBeeLeft = 0.0;
|
||||
if(resultingImgLeft + width > resultingWidth) {
|
||||
resultingWidth = resultingImgLeft + width;
|
||||
}
|
||||
|
||||
const resultingTemplateTop = resultingHeight - template.image.height * templateScale;
|
||||
const resultingTemplateLeft = 0.0;
|
||||
|
||||
var canvas = new Canvas(resultingWidth, resultingHeight);
|
||||
var ctx = canvas.getContext("2d");
|
||||
console.log("Drawing first image")
|
||||
console.log("Drawing template")
|
||||
try {
|
||||
ctx.drawImage(beeTemplate, resultingBeeLeft, resultingBeeTop, resultingWidth, beeTemplate.height * beeScale);
|
||||
ctx.drawImage(template.image, resultingTemplateLeft, resultingTemplateTop, template.image.width * templateScale, template.image.height * templateScale);
|
||||
console.log("Drawing done.")
|
||||
} catch (err) {
|
||||
reject({ status: 400, error: "Invalid template" })
|
||||
}
|
||||
console.log("Drawing second image")
|
||||
console.log("Drawing image")
|
||||
try {
|
||||
ctx.drawImage(img, resultingImgLeft, resultingImgTop, width, height);
|
||||
console.log("Drawing done.")
|
||||
|
@ -94,8 +112,9 @@ const lineWidthScale = 0.2;
|
|||
const lowerRightFactor = 0.5 + 0.7071 * circleScale; // %-ual location of the lower right end of the strikethrough line
|
||||
const upperLeftFactor = 0.5 - 0.7071 * circleScale; // %-ual location of the upper left end of the strikethrough line
|
||||
|
||||
app.get("/bee/", function(req, res) {
|
||||
bee(req.query.url).then((canvas)=>{
|
||||
app.get("/:templateName/", function (req, res) {
|
||||
if(!templates[req.params.templateName]) return res.status(404).end();
|
||||
render(templates[req.params.templateName], req.query.url).then((canvas) => {
|
||||
console.log(canvas)
|
||||
res.setHeader('Content-Type', 'image/png');
|
||||
canvas.pngStream().pipe(res);
|
||||
|
@ -156,13 +175,14 @@ function findEmoji(str) {
|
|||
|
||||
client.on('message', function (message) {
|
||||
console.log(message.cleanContent);
|
||||
if (message.cleanContent.startsWith("/bee")) {
|
||||
const commandParsed = /^\/(\w+)\b/.exec(message.cleanContent);
|
||||
if (commandParsed && templates[commandParsed[1]]) {
|
||||
// get emoji from message
|
||||
const emoji = findEmoji(message.cleanContent);
|
||||
if(emoji) bee(emoji.url).then(canvas => {
|
||||
if (emoji) render(templates[commandParsed[1]], emoji.url).then(canvas => {
|
||||
var messageOptions = {
|
||||
files: [
|
||||
{attachment: canvas.toBuffer(), name: emoji.name.toUpperCase()+"DETECTED.png"}
|
||||
{ attachment: canvas.toBuffer(), name: emoji.name + commandParsed[1] + ".png" }
|
||||
]
|
||||
}
|
||||
message.channel.send("", messageOptions);
|
||||
|
|
Reference in a new issue