feat: add animated choice modes and bracket editor
Verify mini program and deploy preview / verify-deploy (push) Successful in 50s
Verify mini program and deploy preview / verify-deploy (push) Successful in 50s
This commit is contained in:
+86
-14
@@ -1,4 +1,5 @@
|
||||
var api = require('../../utils/api');
|
||||
var posterEditor = require('../../vendor/poster-editor.cjs');
|
||||
|
||||
var THEMES = {
|
||||
celebration: { bg: '#ff6b35', ink: '#fffaf4', card: '#25211f', accent: '#ffe568', label: '庆祝派对' },
|
||||
@@ -16,9 +17,20 @@ Page({
|
||||
{ name: 'sticker', label: '贴纸手账' }
|
||||
],
|
||||
sticker: true,
|
||||
bracketLayout: 'symmetric',
|
||||
bracketView: 'overview',
|
||||
activeTool: 'filter',
|
||||
backgroundPath: '',
|
||||
tools: [
|
||||
{ name: 'filter', label: '滤镜' }, { name: 'text', label: '文字' },
|
||||
{ name: 'sticker', label: '贴纸' }, { name: 'crop', label: '裁剪' },
|
||||
{ name: 'adjust', label: '调整' }, { name: 'layout', label: '布局' },
|
||||
{ name: 'brush', label: '画笔' }
|
||||
],
|
||||
saving: false
|
||||
},
|
||||
onLoad: function (options) {
|
||||
this.posterDocument = posterEditor.createDocument({ id: 'run-' + options.id, seed: options.id, width: 750, height: 1000, meta: { sourceType: 'pk-run', sourceId: options.id } });
|
||||
var cached = xhs.getStorageSync('pk_last_result');
|
||||
this.runId = options.id;
|
||||
if (cached && cached.id === options.id) this.setData({ result: cached });
|
||||
@@ -36,35 +48,45 @@ Page({
|
||||
this.drawPoster();
|
||||
},
|
||||
toggleSticker: function () { this.setData({ sticker: !this.data.sticker }); this.drawPoster(); },
|
||||
pickBracketLayout: function (event) { this.setData({ bracketLayout: event.currentTarget.dataset.layout }); this.drawPoster(); },
|
||||
pickBracketView: function (event) { this.setData({ bracketView: event.currentTarget.dataset.view }); this.drawPoster(); },
|
||||
pickTool: function (event) { this.setData({ activeTool: event.currentTarget.dataset.tool }); },
|
||||
chooseBackground: function () {
|
||||
var self = this;
|
||||
xhs.chooseImage({
|
||||
count: 1,
|
||||
success: function (result) {
|
||||
var path = result.tempFilePaths && result.tempFilePaths[0];
|
||||
if (path) { self.setData({ backgroundPath: path }); self.drawPoster(); }
|
||||
}
|
||||
});
|
||||
},
|
||||
drawPoster: function () {
|
||||
var result = this.data.result;
|
||||
if (!result) return;
|
||||
var theme = THEMES[this.data.template];
|
||||
var isCelebration = this.data.template === 'celebration';
|
||||
var showSticker = this.data.sticker;
|
||||
var layout = this.data.bracketLayout;
|
||||
var view = this.data.bracketView;
|
||||
var backgroundPath = this.data.backgroundPath;
|
||||
xhs.createSelectorQuery().select('.poster').boundingClientRect(function (rect) {
|
||||
var ctx = xhs.createCanvasContext('poster');
|
||||
if (rect) ctx.scale(rect.width / 750, rect.height / 1000);
|
||||
ctx.setFillStyle(theme.bg); ctx.fillRect(0, 0, 750, 1000);
|
||||
if (backgroundPath) {
|
||||
ctx.drawImage(backgroundPath, 0, 0, 750, 1000);
|
||||
ctx.setGlobalAlpha(.82); ctx.setFillStyle(theme.bg); ctx.fillRect(0, 0, 750, 1000); ctx.setGlobalAlpha(1);
|
||||
}
|
||||
ctx.setFillStyle(theme.ink); ctx.setFontSize(24); ctx.fillText('PK 研究所 / MY PICK', 54, 72);
|
||||
ctx.setFontSize(58); ctx.fillText('我的冠军是', 54, 156);
|
||||
ctx.setFillStyle(theme.card); ctx.fillRect(46, 205, 658, 440);
|
||||
ctx.setFillStyle(theme.card); ctx.fillRect(30, 190, 690, 610);
|
||||
ctx.setFillStyle(isCelebration ? '#fffaf4' : theme.ink);
|
||||
ctx.setFontSize(26); ctx.fillText(result.test_title, 84, 270);
|
||||
ctx.setFontSize(result.champion_name.length > 7 ? 62 : 82);
|
||||
ctx.fillText(result.champion_name, 84, 405);
|
||||
ctx.setFillStyle(theme.accent); ctx.fillRect(84, 440, Math.min(500, result.champion_name.length * 80), 18);
|
||||
ctx.setFillStyle(isCelebration ? '#fffaf4' : theme.ink);
|
||||
ctx.setFontSize(24); ctx.fillText('一路二选一,它赢到了最后。', 84, 525);
|
||||
var ranking = result.ranking_names || [];
|
||||
ctx.setFillStyle(theme.ink); ctx.setFontSize(25); ctx.fillText('我的 TOP 4', 54, 720);
|
||||
for (var i = 0; i < Math.min(4, ranking.length); i += 1) {
|
||||
ctx.setFontSize(26); ctx.fillText((i + 1) + '. ' + ranking[i], 64, 774 + i * 48);
|
||||
}
|
||||
ctx.setFontSize(22); ctx.fillText(result.test_title, 54, 235);
|
||||
drawBracket(ctx, result, layout, view, theme, isCelebration);
|
||||
if (showSticker) {
|
||||
ctx.setFillStyle(theme.accent); ctx.beginPath(); ctx.arc(645, 125, 58, 0, Math.PI * 2); ctx.fill();
|
||||
ctx.setFillStyle(theme.card); ctx.setFontSize(28); ctx.fillText('WIN!', 607, 134);
|
||||
ctx.setFontSize(52); ctx.fillText('✦', 640, 700); ctx.fillText('♡', 550, 900);
|
||||
ctx.setFillStyle(theme.card); ctx.setFontSize(24); ctx.fillText('WIN', 615, 134);
|
||||
}
|
||||
ctx.setFillStyle(theme.ink); ctx.setFontSize(20); ctx.fillText('长按保存,把答案分享给同频的人', 54, 956);
|
||||
ctx.draw();
|
||||
@@ -88,3 +110,53 @@ Page({
|
||||
},
|
||||
replay: function () { xhs.redirectTo({ url: '/pages/test/test?slug=' + this.data.result.test_slug }); }
|
||||
});
|
||||
|
||||
function drawBracket(ctx, result, layout, view, theme, isCelebration) {
|
||||
var decisions = result.decisions || [];
|
||||
var names = result.candidate_names || {};
|
||||
if (!decisions.length) {
|
||||
ctx.setFillStyle(isCelebration ? '#fffaf4' : theme.ink);
|
||||
ctx.setFontSize(72);
|
||||
ctx.fillText(result.champion_name, 70, 480);
|
||||
return;
|
||||
}
|
||||
var maxRound = decisions.reduce(function (max, item) { return Math.max(max, item.round); }, 1);
|
||||
var minimumRound = view === 'final' ? maxRound : view === 'top8' ? Math.max(1, maxRound - 2) : view === 'top16' ? Math.max(1, maxRound - 3) : 1;
|
||||
var visible = decisions.filter(function (item) { return item.round >= minimumRound; });
|
||||
if (view === 'left' || view === 'right') {
|
||||
visible = visible.filter(function (item, index) { return item.round === maxRound || index % 2 === (view === 'left' ? 0 : 1); });
|
||||
}
|
||||
var grouped = {};
|
||||
visible.forEach(function (item) { (grouped[item.round] || (grouped[item.round] = [])).push(item); });
|
||||
var positions = {};
|
||||
Object.keys(grouped).forEach(function (roundKey) {
|
||||
var round = Number(roundKey), items = grouped[round], count = items.length;
|
||||
items.forEach(function (item, index) {
|
||||
var centered = layout === 'symmetric';
|
||||
var side = centered && round < maxRound && index % 2 ? 1 : -1;
|
||||
var depth = maxRound - round;
|
||||
var x = centered ? 375 + side * Math.min(250, 82 + depth * 68) : 64 + (round - minimumRound) * (590 / Math.max(1, maxRound - minimumRound));
|
||||
var y = 300 + (index + 1) * (440 / (count + 1));
|
||||
positions[item.winner_id + ':' + item.round] = { x: x, y: y, side: side, item: item };
|
||||
});
|
||||
});
|
||||
ctx.setLineWidth(3);
|
||||
ctx.setStrokeStyle(theme.accent);
|
||||
Object.keys(positions).forEach(function (key) {
|
||||
var pos = positions[key], parent = null;
|
||||
visible.forEach(function (candidate) {
|
||||
if (!parent && candidate.round > pos.item.round && (candidate.left_id === pos.item.winner_id || candidate.right_id === pos.item.winner_id)) parent = positions[candidate.winner_id + ':' + candidate.round];
|
||||
});
|
||||
if (parent) {
|
||||
ctx.beginPath(); ctx.moveTo(pos.x, pos.y); ctx.lineTo((pos.x + parent.x) / 2, pos.y); ctx.lineTo((pos.x + parent.x) / 2, parent.y); ctx.lineTo(parent.x, parent.y); ctx.stroke();
|
||||
}
|
||||
});
|
||||
Object.keys(positions).forEach(function (key) {
|
||||
var pos = positions[key], label = names[pos.item.winner_id] || result.champion_name;
|
||||
ctx.setFillStyle(pos.item.round === maxRound ? theme.accent : theme.bg);
|
||||
ctx.fillRect(pos.x - 58, pos.y - 21, 116, 42);
|
||||
ctx.setFillStyle(theme.ink); ctx.setFontSize(label.length > 6 ? 14 : 17);
|
||||
ctx.fillText(label.slice(0, 8), pos.x - 48, pos.y + 6);
|
||||
});
|
||||
ctx.setFillStyle(theme.ink); ctx.setFontSize(24); ctx.fillText('冠军 ' + result.champion_name, 54, 850);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user