[engine] 支持预缓存 Label Canvas

This commit is contained in:
SmallMain 2024-12-12 21:01:04 +08:00
parent 865e992cb7
commit d212be41ae
No known key found for this signature in database

View File

@ -44,29 +44,46 @@ if(CC_JSB) {
Label._canvasPool = { Label._canvasPool = {
pool: [], pool: [],
used: 0,
max: 32,
get () { get () {
let data = this.pool.pop(); let data = this.pool.pop();
if (!data) { if (!data) {
data = this._create();
}
this.used++;
return data;
},
put (canvas) {
this.used--;
if (this.pool.length >= this.max) {
return;
}
this.pool.push(canvas);
},
_create() {
let canvas = document.createElement("canvas"); let canvas = document.createElement("canvas");
let context = canvas.getContext("2d"); let context = canvas.getContext("2d");
data = {
const data = {
canvas: canvas, canvas: canvas,
context: context context: context
} }
// default text info // default text info
context.textBaseline = 'alphabetic'; context.textBaseline = 'alphabetic';
}
return data; return data;
}, },
put (canvas) { cache(count) {
if (this.pool.length >= 32) { const target = Math.min(this.max, count);
return; let total = this.used + this.pool.length;
} while (total < target) {
this.pool.push(canvas); this.pool.push(this._create());
total++;
} }
},
}; };
Assembler.register(cc.Label, { Assembler.register(cc.Label, {