UNPKG

zent

Version:

一套前端设计语言和基于React的实现

31 lines (26 loc) 981 B
import createElement from '../../utils/dom/createElement'; const checkboardCache = {}; export function render(c1, c2, size, ServerCanvas) { if (typeof document === 'undefined' && !ServerCanvas) return null; const canvas = ServerCanvas ? new ServerCanvas() : createElement('canvas'); canvas.width = size * 2; canvas.height = size * 2; const ctx = canvas.getContext('2d'); if (!ctx) return null; // If no context can be found, return early. ctx.fillStyle = c1; ctx.fillRect(0, 0, canvas.width, canvas.height); ctx.fillStyle = c2; ctx.fillRect(0, 0, size, size); ctx.translate(size, size); ctx.fillRect(0, 0, size, size); return canvas.toDataURL(); } export function get(c1, c2, size, serverCanvas) { const key = `${c1}-${c2}-${size}${serverCanvas ? '-server' : ''}`; const checkboard = render(c1, c2, size, serverCanvas); if (checkboardCache[key]) { return checkboardCache[key]; } checkboardCache[key] = checkboard; return checkboard; }