From d212be41aee8396202e826f8ad0176a5874b6993 Mon Sep 17 00:00:00 2001 From: SmallMain Date: Thu, 12 Dec 2024 21:01:04 +0800 Subject: [PATCH] =?UTF-8?q?[engine]=20=E6=94=AF=E6=8C=81=E9=A2=84=E7=BC=93?= =?UTF-8?q?=E5=AD=98=20Label=20Canvas?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../renderer/webgl/assemblers/label/index.js | 39 +++++++++++++------ 1 file changed, 28 insertions(+), 11 deletions(-) diff --git a/engine/cocos2d/core/renderer/webgl/assemblers/label/index.js b/engine/cocos2d/core/renderer/webgl/assemblers/label/index.js index 5e3da645..04964a07 100644 --- a/engine/cocos2d/core/renderer/webgl/assemblers/label/index.js +++ b/engine/cocos2d/core/renderer/webgl/assemblers/label/index.js @@ -44,29 +44,46 @@ if(CC_JSB) { Label._canvasPool = { pool: [], + used: 0, + max: 32, get () { let data = this.pool.pop(); if (!data) { - let canvas = document.createElement("canvas"); - let context = canvas.getContext("2d"); - data = { - canvas: canvas, - context: context - } - - // default text info - context.textBaseline = 'alphabetic'; + data = this._create(); } + this.used++; return data; }, put (canvas) { - if (this.pool.length >= 32) { + this.used--; + if (this.pool.length >= this.max) { return; } this.pool.push(canvas); - } + }, + _create() { + let canvas = document.createElement("canvas"); + let context = canvas.getContext("2d"); + + const data = { + canvas: canvas, + context: context + } + + // default text info + context.textBaseline = 'alphabetic'; + return data; + }, + cache(count) { + const target = Math.min(this.max, count); + let total = this.used + this.pool.length; + while (total < target) { + this.pool.push(this._create()); + total++; + } + }, }; Assembler.register(cc.Label, {