Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
5000兆円ジェネレーター(node.js)
const fs = require('fs');
const Canvas = require('canvas');
var offset = { top: {x: 0, y: 0}, bottom: {x: 250, y: 130} };
var actualWidth = { top: 0, bottom: 0 };
if (process.argv.length < 3) {
console.error('Usage: 5000choyen.js "text1" ["text2"]');
process.exit(1);
}
const text1 = process.argv[2];
var isExistsText2 = typeof process.argv[3] !== 'undefined';
const text2 = isExistsText2 ? process.argv[3] : '';
var canvas = Canvas.createCanvas(3000, 280);
var ctx = canvas.getContext('2d');
ctx.lineJoin = 'round';
ctx.fillStyle = 'white';
Canvas.registerFont(__dirname + '/notobk-subset.otf', {family: 'notobk'});
Canvas.registerFont(__dirname + '/notoserifbk-subset.otf', {family: 'notoserifbk'});
redrawTop(text1);
if (isExistsText2) {
redrawBottom(text2);
}
adjustCanvas(isExistsText2)
canvas.toDataURL('image/jpeg', 0.9, function(err, jpeg){ console.log(jpeg) });
// ソース参考サイト: https://auri.ga/5000generator/
function adjustCanvas(isExistsBottom) {
var width = Math.max(actualWidth.top, actualWidth.bottom);
var imageData = ctx.getImageData(0, 0, width, canvas.height);
canvas.width = imageData.width + 10;
if (isExistsBottom) {
canvas.height = imageData.height - 10;
} else {
canvas.height = imageData.height / 2 - 10;
}
ctx.fillStyle = 'white';
ctx.fillRect(0, 0, canvas.width, canvas.height + 100);
ctx.putImageData(imageData, 0, 0);
}
function redrawTop(text) {
ctx.setTransform(1,0,-0.4,1,0,0);
ctx.font = '100px notobk';
ctx.fillStyle = "white";
ctx.fillRect(0, 0, canvas.width, canvas.height/2);
var posx = 70;
var posy = 100;
//黒色
{
ctx.strokeStyle = "#000";
ctx.lineWidth = 22;
ctx.strokeText(text, posx + 4, posy + 4);
}
//銀色
{
var grad = ctx.createLinearGradient(0, 24, 0, 122);
grad.addColorStop(0.0, 'rgb(0,15,36)');
grad.addColorStop(0.10, 'rgb(255,255,255)');
grad.addColorStop(0.18, 'rgb(55,58,59)');
grad.addColorStop(0.25, 'rgb(55,58,59)');
grad.addColorStop(0.5, 'rgb(200,200,200)');
grad.addColorStop(0.75, 'rgb(55,58,59)');
grad.addColorStop(0.85, 'rgb(25,20,31)');
grad.addColorStop(0.91, 'rgb(240,240,240)');
grad.addColorStop(0.95, 'rgb(166,175,194)');
grad.addColorStop(1, 'rgb(50,50,50)');
ctx.strokeStyle = grad;
ctx.lineWidth = 20;
ctx.strokeText(text, posx + 4, posy + 4);
}
//黒色
{
ctx.strokeStyle = "#000000";
ctx.lineWidth = 16;
ctx.strokeText(text, posx, posy);
}
//金色
{
var grad = ctx.createLinearGradient(0, 20, 0, 100);
grad.addColorStop(0, 'rgb(253,241,0)');
grad.addColorStop(0.25, 'rgb(245,253,187)');
grad.addColorStop(0.4, 'rgb(255,255,255)');
grad.addColorStop(0.75, 'rgb(253,219,9)');
grad.addColorStop(0.9, 'rgb(127,53,0)');
grad.addColorStop(1, 'rgb(243,196,11)');
ctx.strokeStyle = grad;
ctx.lineWidth = 10;
ctx.strokeText(text, posx, posy);
}
//黒
ctx.lineWidth = 6;
ctx.strokeStyle = '#000';
ctx.strokeText(text, posx+2, posy - 3);
//白
ctx.lineWidth = 6;
ctx.strokeStyle = '#FFFFFF';
ctx.strokeText(text, posx, posy - 3);
//赤
{
var grad = ctx.createLinearGradient(0, 20, 0, 100);
grad.addColorStop(0, 'rgb(255, 100, 0)');
grad.addColorStop(0.5, 'rgb(123, 0, 0)');
grad.addColorStop(0.51, 'rgb(240, 0, 0)');
grad.addColorStop(1, 'rgb(5, 0, 0)');
ctx.lineWidth = 4;
ctx.strokeStyle = grad;
ctx.strokeText(text, posx, posy - 3);
}
//赤
{
var grad = ctx.createLinearGradient(0, 20, 0, 100);
grad.addColorStop(0, 'rgb(230, 0, 0)');
grad.addColorStop(0.5, 'rgb(123, 0, 0)');
grad.addColorStop(0.51, 'rgb(240, 0, 0)');
grad.addColorStop(1, 'rgb(5, 0, 0)');
ctx.fillStyle = grad;
ctx.fillText(text, posx, posy - 3);
}
actualWidth.top = ctx.measureText(text).width + posx;
}
function redrawBottom(text, offsetX) {
var offsetX = offsetX || offset.bottom.x;
var offsetY = offset.bottom.y;
ctx.setTransform(1, 0, -0.4, 1, 0, 0);
ctx.font = '100px notoserifbk';
ctx.fillStyle = "white";
ctx.fillRect(0, 130, canvas.width, canvas.height/2);
var posx = 70 + offsetX;
var posy = 100 + offsetY;
//黒色
{
ctx.strokeStyle = "#000";
ctx.lineWidth = 22;
ctx.strokeText(text, posx + 5, posy + 2);
}
// 銀
{
var grad = ctx.createLinearGradient(0+offsetX, 20+offsetY, 0+offsetX, 118+offsetY);
grad.addColorStop(0, 'rgb(0,15,36)');
grad.addColorStop(0.25, 'rgb(250,250,250)');
grad.addColorStop(0.5, 'rgb(150,150,150)');
grad.addColorStop(0.75, 'rgb(55,58,59)');
grad.addColorStop(0.85, 'rgb(25,20,31)');
grad.addColorStop(0.91, 'rgb(240,240,240)');
grad.addColorStop(0.95, 'rgb(166,175,194)');
grad.addColorStop(1, 'rgb(50,50,50)');
ctx.strokeStyle = grad;
ctx.lineWidth = 19;
ctx.strokeText(text, posx + 5, posy + 2);
}
//黒色
{
ctx.strokeStyle = "#10193A";
ctx.lineWidth = 17;
ctx.strokeText(text, posx, posy);
}
// 白
{
ctx.strokeStyle = "#DDD";
ctx.lineWidth = 8;
ctx.strokeText(text, posx, posy);
}
//紺
{
var grad = ctx.createLinearGradient(0+offsetX, 20+offsetY, 0+offsetX, 100+offsetY);
grad.addColorStop(0, 'rgb(16,25,58)');
grad.addColorStop(0.03, 'rgb(255,255,255)');
grad.addColorStop(0.08, 'rgb(16,25,58)');
grad.addColorStop(0.2, 'rgb(16,25,58)');
grad.addColorStop(1, 'rgb(16,25,58)');
ctx.strokeStyle = grad;
ctx.lineWidth = 7;
ctx.strokeText(text, posx, posy);
}
//銀
{
var grad = ctx.createLinearGradient(0+offsetX, 20+offsetY, 0+offsetX, 100+offsetY);
grad.addColorStop(0, 'rgb(245,246,248)');
grad.addColorStop(0.15, 'rgb(255,255,255)');
grad.addColorStop(0.35, 'rgb(195,213,220)');
grad.addColorStop(0.5, 'rgb(160,190,201)');
grad.addColorStop(0.51, 'rgb(160,190,201)');
grad.addColorStop(0.52, 'rgb(196,215,222)');
grad.addColorStop(1.0, 'rgb(255,255,255)');
ctx.fillStyle = grad;
ctx.fillText(text, posx, posy - 3);
}
// textWidth = Math.maox(ctx.measureText(text).width+offsetX, textWidth+offsetX);
actualWidth.bottom = ctx.measureText(text).width + posx;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.