Skip to content

Commit

Permalink
debug: logging in scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
sorae42 committed Nov 24, 2023
1 parent 7d6f78d commit ba9f448
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 8 deletions.
14 changes: 9 additions & 5 deletions scripts/card.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ logo.onload = redrawBg;
// for the card canvas
function redrawBg() {
// asset calculations
console.log(`[call::redrawBg()] showLogo:${showLogo} showWtm:${showWtm}`);

const logoScale = document.querySelector('#logo-size-option').value;
const logoOffset = document.querySelector('#logo-offset').value;

Expand Down Expand Up @@ -56,16 +58,18 @@ function redrawText() {
const delay = Number(document.querySelector('#delay-rate > input[type="number"]').value);
const fontSize = Math.min(Math.abs(+fontSizeInput.value || 120));
const fontFamily = fontFamilyInput.value || 'sans-serif';
const value = (textInput.value || 'TAKE YOUR HEART').trim();
const splitValue = value.split('\n');

console.log(`[call::redrawText()] textInput:${value}`);
console.log(`[call::redrawText()] delay:${delay} fontSize:${fontSize} fontFamily:${fontFamily}`);

// another canvas so making multiline text is easier
lineCanvas.width = canvasText.width;
lineCanvas.height = fontSize * 2.2;

textCtx.clearRect(0, 0, canvasText.width, canvasText.height);

const value = (textInput.value || 'TAKE YOUR HEART').trim();
const splitValue = value.split('\n');


// they are all offset, just a different name and purpose
let lineHeight = 0, middleOffset = 0, heightOffset = 0;
let topOffset = Number(document.querySelector('#text-top').value);
Expand All @@ -88,7 +92,7 @@ function redrawText() {
textCtx.drawImage(lineCanvas, 0, lineHeight + middleOffset + topOffset);

lineHeight = Math.floor(heightOffset) || lineHeight;
console.log(value, lineHeight, middleOffset, heightOffset);
console.log(`[call::redrawText()] lineHeight:${lineHeight} middleOffset:${middleOffset} heightOffset:${heightOffset}`);
}, timer);
timer += delay;
});
Expand Down
1 change: 1 addition & 0 deletions scripts/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ function saveDelay() {
date.setTime(date.getTime() + (365 * 24 * 60 * 60 * 1000));
let expires = "expires=" + date.toUTCString();
document.cookie = `delay=${delay};${expires};path=/`;
console.log(`[call::saveDelay()] delay:${delay}ms OK`);
}

window.onload = function() {
Expand Down
18 changes: 17 additions & 1 deletion scripts/exporter.js
Original file line number Diff line number Diff line change
@@ -1 +1,17 @@
function exportCard(){let e=document.querySelector("#canvas-card"),a=document.querySelector("#canvas-text");redrawBg();let t=e.getContext("2d");t.save(),t.drawImage(a,0,0);const r=canvas.toDataURL("image/png").replace("image/png","image/octet-stream"),c=document.createElement("a");c.href=r,c.download=`p5cc_${Math.floor(1e6*Math.random())}.png`,c.target="blank",c.click(),t.restore()}
function exportCard() {
console.log("[call::exportCard()] Saving image...");
let e = document.querySelector("#canvas-card"),
a = document.querySelector("#canvas-text");
redrawBg();
let t = e.getContext("2d");
t.save(), t.drawImage(a, 0, 0);
const r = canvas
.toDataURL("image/png")
.replace("image/png", "image/octet-stream"),
c = document.createElement("a");
(c.href = r),
(c.download = `p5cc_${Math.floor(1e6 * Math.random())}.png`),
(c.target = "blank"),
c.click(),
t.restore();
}
3 changes: 2 additions & 1 deletion scripts/textgen/text.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@ class BoxText {
: this.chars.push(new BoxChar(e, i[t], this.fontSize, this.fontFamily));
}
draw(t) {
console.log("[BoxText.call::draw()]");
const e = t.getContext("2d");
if (!e) return void console.error("Failed to load canvas");
if (!e) return void console.error("[BoxText.call::draw()] Failed to load canvas");
e.clearRect(0, 0, t.width, t.height);
const o = this.pendding,
i = this.gutter;
Expand Down
5 changes: 4 additions & 1 deletion scripts/textgen/utils.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
function randomOper() {
return Math.floor(10 * Math.random()) % 2 ? 1 : -1;
}

function rotateCanvas(t, e, n, a) {
t.translate(n, a), t.rotate((Math.PI * e) / 180), t.translate(-n, -a);
}

function letterCanvas(t, e) {
const n = document.createElement("canvas");
(n.width = t), (n.height = e);
const a = n.getContext("2d");
if (a) return { canvas: n, context: a };
console.error(
"Failed to generate text: failed to create canvas for the letter."
"[call::letterCanvas()] Failed to generate text: failed to create canvas for the letter."
);
}

function getCharSize(t, e, n = "sans-serif", a = "normal") {
const { context: o } = letterCanvas(e, e);
(o.font = `${a} ${e}px ${n}`), (o.textBaseline = "top"), o.fillText(t, 0, 0);
Expand Down

0 comments on commit ba9f448

Please sign in to comment.