feat: share choice modes and bracket editor
Verify mini program and deploy preview / verify-deploy (push) Successful in 25s
Verify mini program and deploy preview / verify-deploy (push) Successful in 25s
This commit is contained in:
+24
-1
@@ -1 +1,24 @@
|
||||
.kicker{text-align:center;color:#ff6b35;font-size:22rpx;letter-spacing:6rpx;font-weight:900}.headline{margin:12rpx 0 28rpx;text-align:center;font-size:48rpx;font-weight:900}.champion{position:relative;overflow:hidden;padding:54rpx 32rpx;text-align:center;border-radius:38rpx;background:#25211f;color:#fff}.crown{position:absolute;right:22rpx;top:0;color:#ffe568;font-size:100rpx;transform:rotate(13deg)}.muted-light{opacity:.6;font-size:23rpx}.winner{margin-top:16rpx;font-size:72rpx;font-weight:900}.runner{margin-top:20rpx;color:#ded4ce;font-size:23rpx}.poster-head{display:flex;justify-content:space-between;margin:46rpx 4rpx 20rpx;font-size:30rpx;font-weight:900}.poster{width:100%;height:925rpx;border-radius:30rpx;box-shadow:0 16rpx 40rpx rgba(50,30,20,.12)}.themes{display:flex;margin-top:24rpx;overflow-x:auto}.themes button{flex:none;margin-right:12rpx;padding:0 24rpx;height:66rpx;line-height:66rpx;border-radius:99rpx;background:#fff;color:#635951;font-size:22rpx}.themes .active{background:#25211f;color:#fff}.sticker{display:flex;justify-content:space-between;margin-top:24rpx;padding:24rpx;border-radius:22rpx;background:#fff}.action{margin-top:20rpx;font-size:28rpx}
|
||||
.editor { min-height: 100vh; padding: 20rpx 22rpx 40rpx; background: #151515; color: #fff; }
|
||||
.editor-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 18rpx; }
|
||||
.editor-head text { font-size: 31rpx; font-weight: 900; }
|
||||
.editor-head view view { margin-top: 4rpx; color: #888; font-size: 19rpx; }
|
||||
.export { margin: 0; padding: 0 30rpx; min-width: 0; height: 64rpx; line-height: 64rpx; border-radius: 99rpx; background: #ff3156; color: #fff; font-size: 23rpx; font-weight: 800; }
|
||||
.poster { width: 100%; height: 880rpx; background: #242424; }
|
||||
.strip, .media-strip, .tool-strip { white-space: nowrap; }
|
||||
.strip-row, .media-row, .tool-row { display: flex; align-items: center; }
|
||||
.strip { margin-top: 18rpx; }
|
||||
.chip { flex: none; margin-right: 10rpx; padding: 0 22rpx; min-width: 0; height: 54rpx; line-height: 54rpx; border-radius: 8rpx; background: #292929; color: #aaa; font-size: 20rpx; }
|
||||
.chip.active { background: #fff; color: #111; }
|
||||
.media-strip { margin-top: 22rpx; }
|
||||
.media-thumb, .add-media { flex: none; display: flex; flex-direction: column; align-items: center; justify-content: center; width: 112rpx; height: 112rpx; margin-right: 12rpx; border: 2rpx solid #444; border-radius: 10rpx; background: #252525; color: #fff; font-size: 20rpx; }
|
||||
.bracket-thumb { border-color: #fff; background: #f3eadf; color: #111; font-weight: 800; }
|
||||
.add-media text { margin-top: 5rpx; color: #999; font-size: 16rpx; }
|
||||
.tool-strip { margin-top: 25rpx; }
|
||||
.tool { flex: none; width: 104rpx; text-align: center; color: #888; font-size: 19rpx; }
|
||||
.tool.active { color: #fff; }
|
||||
.tool-icon { display: grid; place-items: center; width: 50rpx; height: 50rpx; margin: 0 auto 9rpx; border: 3rpx solid currentColor; border-radius: 12rpx; font-size: 18rpx; }
|
||||
.tool-panel { display: flex; margin-top: 22rpx; padding: 18rpx; overflow: hidden; background: #222; }
|
||||
.theme-chip { flex: 1; margin: 0 5rpx; padding: 0; height: 58rpx; line-height: 58rpx; border-radius: 6rpx; background: #333; color: #aaa; font-size: 19rpx; }
|
||||
.theme-chip.active { background: #fff; color: #111; }
|
||||
.setting-row { display: flex; width: 100%; justify-content: space-between; font-size: 22rpx; }
|
||||
.hint { color: #777; font-size: 20rpx; }
|
||||
|
||||
+160
-18
@@ -1,20 +1,162 @@
|
||||
var api=require('../../utils/api');
|
||||
var THEMES={celebration:{bg:'#ff6b35',ink:'#fffaf4',card:'#25211f',accent:'#ffe568'},editorial:{bg:'#f3eadf',ink:'#201d1b',card:'#fff',accent:'#285ee8'},sticker:{bg:'#c9f06b',ink:'#1e2810',card:'#ff72a7',accent:'#fff'}};
|
||||
var api = require('../../utils/api');
|
||||
var posterEditor = require('../../vendor/poster-editor.cjs');
|
||||
|
||||
var THEMES = {
|
||||
celebration: { bg: '#ff6b35', ink: '#fffaf4', card: '#25211f', accent: '#ffe568', label: '庆祝派对' },
|
||||
editorial: { bg: '#f3eadf', ink: '#201d1b', card: '#ffffff', accent: '#285ee8', label: '杂志封面' },
|
||||
sticker: { bg: '#c9f06b', ink: '#1e2810', card: '#ff72a7', accent: '#ffffff', label: '贴纸手账' }
|
||||
};
|
||||
|
||||
Page({
|
||||
data:{result:null,template:'celebration',sticker:true,saving:false},
|
||||
onLoad:function(options){this.runId=options.id;var cached=xhs.getStorageSync('rice_last_result');if(cached&&cached.id===options.id)this.setData({result:cached});else this.load();},
|
||||
onReady:function(){if(this.data.result)this.draw();},
|
||||
load:function(){var self=this;api.ensureLogin().then(function(){return api.request('/api/v1/me/results/'+self.runId);}).then(function(result){self.setData({result:result});self.draw();}).catch(function(error){xhs.showModal({title:'读取失败',content:error.message||'请重试',showCancel:false});});},
|
||||
pick:function(event){this.setData({template:event.currentTarget.dataset.name});this.draw();},
|
||||
toggle:function(){this.setData({sticker:!this.data.sticker});this.draw();},
|
||||
draw:function(){
|
||||
var r=this.data.result;if(!r)return;
|
||||
var t=THEMES[this.data.template],isCelebration=this.data.template==='celebration',showSticker=this.data.sticker;
|
||||
xhs.createSelectorQuery().select('.poster').boundingClientRect(function(rect){
|
||||
var ctx=xhs.createCanvasContext('poster');if(rect)ctx.scale(rect.width/750,rect.height/1000);
|
||||
ctx.setFillStyle(t.bg);ctx.fillRect(0,0,750,1000);ctx.setFillStyle(t.ink);ctx.setFontSize(24);ctx.fillText('RICE PK / MY FINAL PICK',54,72);ctx.setFontSize(58);ctx.fillText('我的本命饭',54,156);ctx.setFillStyle(t.card);ctx.fillRect(46,205,658,440);ctx.setFillStyle(isCelebration?'#fffaf4':t.ink);ctx.setFontSize(26);ctx.fillText('48 种饭 · 一路二选一',84,270);ctx.setFontSize(r.champion_name.length>7?62:82);ctx.fillText(r.champion_name,84,405);ctx.setFillStyle(t.accent);ctx.fillRect(84,440,Math.min(500,r.champion_name.length*80),18);ctx.setFillStyle(isCelebration?'#fffaf4':t.ink);ctx.setFontSize(24);ctx.fillText('今天就吃它,不接受反驳。',84,525);ctx.setFillStyle(t.ink);ctx.setFontSize(25);ctx.fillText('我的 TOP 4',54,720);(r.ranking_names||[]).slice(0,4).forEach(function(name,index){ctx.setFontSize(26);ctx.fillText((index+1)+'. '+name,64,774+index*48);});if(showSticker){ctx.setFillStyle(t.accent);ctx.beginPath();ctx.arc(645,125,58,0,Math.PI*2);ctx.fill();ctx.setFillStyle(t.card);ctx.setFontSize(28);ctx.fillText('开饭!',607,134);ctx.setFontSize(52);ctx.fillText('✦',640,700);ctx.fillText('♡',550,900);}ctx.setFillStyle(t.ink);ctx.setFontSize(20);ctx.fillText('保存分享图,看看朋友会选哪一碗',54,956);ctx.draw();
|
||||
}).exec();
|
||||
},
|
||||
save:function(){var self=this;this.setData({saving:true});xhs.canvasToTempFilePath({canvasId:'poster',destWidth:750,destHeight:1000,success:function(file){xhs.saveImageToPhotosAlbum({filePath:file.tempFilePath,success:function(){xhs.showToast({title:'已保存到相册'});},fail:function(){xhs.showToast({title:'请检查相册权限',icon:'none'});},complete:function(){self.setData({saving:false});}});},fail:function(){self.setData({saving:false});xhs.showToast({title:'生成失败',icon:'none'});}});},
|
||||
replay:function(){xhs.redirectTo({url:'/pages/test/test'});}
|
||||
data: {
|
||||
result: null,
|
||||
template: 'celebration',
|
||||
themeList: [
|
||||
{ name: 'celebration', label: '庆祝派对' },
|
||||
{ name: 'editorial', label: '杂志封面' },
|
||||
{ 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('rice_last_result');
|
||||
this.runId = options.id;
|
||||
if (cached && cached.id === options.id) this.setData({ result: cached });
|
||||
else this.load();
|
||||
},
|
||||
onReady: function () { if (this.data.result) this.drawPoster(); },
|
||||
load: function () {
|
||||
var self = this;
|
||||
api.ensureLogin().then(function () { return api.request('/api/v1/me/results/' + self.runId); }).then(function (result) {
|
||||
self.setData({ result: result }); self.drawPoster();
|
||||
}).catch(function (error) { xhs.showModal({ title: '读取失败', content: error.message || '请稍后重试', showCancel: false }); });
|
||||
},
|
||||
pickTemplate: function (event) {
|
||||
this.setData({ template: event.currentTarget.dataset.name });
|
||||
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(30, 190, 690, 610);
|
||||
ctx.setFillStyle(isCelebration ? '#fffaf4' : theme.ink);
|
||||
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(24); ctx.fillText('WIN', 615, 134);
|
||||
}
|
||||
ctx.setFillStyle(theme.ink); ctx.setFontSize(20); ctx.fillText('长按保存,把答案分享给同频的人', 54, 956);
|
||||
ctx.draw();
|
||||
}).exec();
|
||||
},
|
||||
savePoster: function () {
|
||||
var self = this;
|
||||
this.setData({ saving: true });
|
||||
xhs.canvasToTempFilePath({
|
||||
canvasId: 'poster', destWidth: 750, destHeight: 1000,
|
||||
success: function (file) {
|
||||
xhs.saveImageToPhotosAlbum({
|
||||
filePath: file.tempFilePath,
|
||||
success: function () { xhs.showToast({ title: '已保存到相册' }); },
|
||||
fail: function () { xhs.showToast({ title: '保存失败,请检查相册权限', icon: 'none' }); },
|
||||
complete: function () { self.setData({ saving: false }); }
|
||||
});
|
||||
},
|
||||
fail: function () { self.setData({ saving: false }); xhs.showToast({ title: '海报生成失败', icon: 'none' }); }
|
||||
});
|
||||
},
|
||||
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);
|
||||
}
|
||||
|
||||
@@ -1,2 +1,18 @@
|
||||
<view class="page" xhs:if="{{result}}"><view class="kicker">FINAL RICE</view><view class="headline">本命饭出炉!</view><view class="champion"><view class="crown">♛</view><view class="muted-light">48 种饭的最终冠军</view><view class="winner">{{result.champion_name}}</view><view class="runner" xhs:if="{{result.runner_up_name}}">决赛击败 {{result.runner_up_name}}</view></view><view class="poster-head"><text>分享卡片</text><text class="muted">3 款模板</text></view><canvas canvas-id="poster" class="poster"></canvas><view class="themes"><button class="{{template==='celebration'?'active':''}}" data-name="celebration" bindtap="pick">庆祝派对</button><button class="{{template==='editorial'?'active':''}}" data-name="editorial" bindtap="pick">杂志封面</button><button class="{{template==='sticker'?'active':''}}" data-name="sticker" bindtap="pick">贴纸手账</button></view><view class="sticker" bindtap="toggle"><text>手绘贴纸</text><text>{{sticker?'开启 ✓':'关闭'}}</text></view><button class="primary action" loading="{{saving}}" bindtap="save">保存分享图</button><button class="secondary action" bindtap="replay">再选一次</button></view>
|
||||
|
||||
<view class="page editor" xhs:if="{{result}}">
|
||||
<view class="editor-head"><view><text>分享图编辑器</text><view>{{result.test_title}} · {{result.champion_name}}</view></view><button class="export" loading="{{saving}}" bindtap="savePoster">导出</button></view>
|
||||
<canvas canvas-id="poster" class="poster"></canvas>
|
||||
<scroll-view scroll-x class="strip"><view class="strip-row">
|
||||
<button class="chip {{bracketLayout === 'symmetric' ? 'active' : ''}}" data-layout="symmetric" bindtap="pickBracketLayout">对称图</button>
|
||||
<button class="chip {{bracketLayout === 'single' ? 'active' : ''}}" data-layout="single" bindtap="pickBracketLayout">单边图</button>
|
||||
<button class="chip {{bracketView === 'overview' ? 'active' : ''}}" data-view="overview" bindtap="pickBracketView">总览</button>
|
||||
<button class="chip" data-view="left" bindtap="pickBracketView">左半区</button><button class="chip" data-view="right" bindtap="pickBracketView">右半区</button>
|
||||
<button class="chip" data-view="top16" bindtap="pickBracketView">16 强</button><button class="chip" data-view="top8" bindtap="pickBracketView">8 强</button><button class="chip" data-view="final" bindtap="pickBracketView">决赛</button>
|
||||
</view></scroll-view>
|
||||
<scroll-view scroll-x class="media-strip"><view class="media-row"><view class="media-thumb bracket-thumb">晋级图</view><button class="add-media" bindtap="chooseBackground">+<text>添加底图</text></button></view></scroll-view>
|
||||
<scroll-view scroll-x class="tool-strip"><view class="tool-row">
|
||||
<view xhs:for="{{tools}}" xhs:key="name" class="tool {{activeTool === item.name ? 'active' : ''}}" data-tool="{{item.name}}" bindtap="pickTool"><view class="tool-icon">{{index + 1}}</view><text>{{item.label}}</text></view>
|
||||
</view></scroll-view>
|
||||
<view class="tool-panel" xhs:if="{{activeTool === 'filter'}}"><button xhs:for="{{themeList}}" xhs:key="name" class="theme-chip {{template === item.name ? 'active' : ''}}" data-name="{{item.name}}" bindtap="pickTemplate">{{item.label}}</button></view>
|
||||
<view class="tool-panel" xhs:elif="{{activeTool === 'sticker'}}"><view class="setting-row" bindtap="toggleSticker"><text>装饰贴纸</text><text>{{sticker ? '已开启' : '关闭'}}</text></view></view>
|
||||
<view class="tool-panel hint" xhs:else>此工具将由 @entroia/poster-editor 的图层绑定驱动</view>
|
||||
</view>
|
||||
|
||||
Reference in New Issue
Block a user