discourse/app/assets/javascripts/wizard/components/image-preview-logo-small.js...

76 lines
1.9 KiB
Plaintext
Raw Normal View History

2018-06-15 11:03:24 -04:00
import { observes } from "ember-addons/ember-computed-decorators";
2016-09-16 16:39:12 -04:00
2018-06-15 11:03:24 -04:00
import { createPreviewComponent, drawHeader, LOREM } from "wizard/lib/preview";
2016-09-16 16:39:12 -04:00
export default createPreviewComponent(375, 100, {
image: null,
2018-06-15 11:03:24 -04:00
@observes("field.value")
2016-09-16 16:39:12 -04:00
imageChanged() {
this.reload();
},
images() {
2018-06-15 11:03:24 -04:00
return { image: this.get("field.value") };
2016-09-16 16:39:12 -04:00
},
paint(ctx, colors, width, height) {
const headerHeight = height / 2;
drawHeader(ctx, colors, width, headerHeight);
const image = this.image;
const headerMargin = headerHeight * 0.2;
2018-06-15 11:03:24 -04:00
const maxWidth = headerHeight - headerMargin * 2.0;
2016-09-16 16:39:12 -04:00
let imageWidth = image.width;
let ratio = 1.0;
if (imageWidth > maxWidth) {
ratio = maxWidth / imageWidth;
imageWidth = maxWidth;
}
2018-06-15 11:03:24 -04:00
this.scaleImage(
image,
headerMargin,
headerMargin,
imageWidth,
image.height * ratio
);
2016-09-16 16:39:12 -04:00
2018-06-15 11:03:24 -04:00
const afterLogo = headerMargin * 1.7 + imageWidth;
2016-09-16 16:39:12 -04:00
const fontSize = Math.round(headerHeight * 0.4);
ctx.font = `Bold ${fontSize}px 'Arial'`;
ctx.fillStyle = colors.primary;
const title = LOREM.substring(0, 27);
2018-06-15 11:03:24 -04:00
ctx.fillText(
title,
headerMargin + imageWidth,
headerHeight - fontSize * 1.1
);
2016-09-16 16:39:12 -04:00
const category = this.categories()[0];
2016-09-16 16:39:12 -04:00
const badgeSize = height / 13.0;
ctx.beginPath();
ctx.fillStyle = category.color;
2018-06-15 11:03:24 -04:00
ctx.rect(afterLogo, headerHeight * 0.7, badgeSize, badgeSize);
2016-09-16 16:39:12 -04:00
ctx.fill();
ctx.font = `Bold ${badgeSize * 1.2}px 'Arial'`;
ctx.fillStyle = colors.primary;
2018-06-15 11:03:24 -04:00
ctx.fillText(
category.name,
afterLogo + badgeSize * 1.5,
headerHeight * 0.7 + badgeSize * 0.9
);
2016-09-16 16:39:12 -04:00
const LINE_HEIGHT = 12;
ctx.font = `${LINE_HEIGHT}px 'Arial'`;
const lines = LOREM.split("\n");
2018-06-15 11:03:24 -04:00
for (let i = 0; i < 10; i++) {
const line = height * 0.55 + i * (LINE_HEIGHT * 1.5);
2016-09-16 16:39:12 -04:00
ctx.fillText(lines[i], afterLogo, line);
}
}
});