ข้อมูลอัปเดตเกี่ยวกับ HTML ใน Canvas: การทำซ้ำเพื่อมุ่งสู่ Web API ที่ดียิ่งขึ้น

Chrome 150 และ 155 มีการเปลี่ยนแปลงหลายอย่างใน HTML-in-Canvas API การเปลี่ยนแปลงเหล่านี้เป็นการตอบสนองต่อความคิดเห็นของชุมชนและมาตรฐานโดยมีเป้าหมายเพื่อปรับปรุงหลักสรีรศาสตร์ของ API บล็อกโพสต์นี้จะสรุปการเปลี่ยนแปลงและวิธีตรวจสอบว่าการติดตั้งใช้งานของคุณเป็นเวอร์ชันล่าสุด

การอัปเดตประกอบด้วยการเปลี่ยนชื่อแอตทริบิวต์และเมธอด การกำหนดให้ต้องจัดสรรหน่วยความจำพื้นผิวล่วงหน้าอย่างชัดเจน และการอัปเดตโมเดลการซิงค์ DOM นอกจากนี้ ช่วงทดลองใช้จากต้นทางของ HTML-in-Canvas ได้รับการขยายเวลาแล้ว ต่อไปนี้เป็นคำอธิบายเกี่ยวกับการเปลี่ยนแปลง เหตุผล และวิธีอัปเดตฐานของโค้ด

การเปลี่ยนแปลงใน Chrome 150

ใน Chrome 150 เราได้เปลี่ยนซิกเนเจอร์อาร์กิวเมนต์ใน WebGL และ WebGPU เพื่อกำจัดความซ้ำซ้อนและสอดคล้องกับรูปแบบกราฟิกสมัยใหม่

WebGL

เรานำอาร์กิวเมนต์ที่ซ้ำซ้อน (level, srcFormat, destType) ออกจาก texElementImage2D (ดูรายละเอียดเพิ่มเติมได้ที่การสนทนาเกี่ยวกับ WebGL)

// Old implementation (Chrome < 150)
const level = 0;
const internalFormat = gl.RGBA;
const srcFormat = gl.RGBA;
const destType = gl.UNSIGNED_BYTE;

gl.texElementImage2D(
  gl.TEXTURE_2D,
  level,
  internalFormat,
  srcFormat,
  destType,
  element
);

// New implementation (from Chrome 150)
const internalFormat = gl.RGBA8;
gl.texElementImage2D(gl.TEXTURE_2D, internalFormat, element);

WebGPU

เราได้กำหนดมาตรฐานพารามิเตอร์ copyElementImageToTexture เป็นออบเจ็กต์ sourceDict และ destDict (ดู GPUWeb PR#6250) ดังนี้

// Old implementation (Chrome < 150)
GPUQueue.copyElementImageToTexture(
  element,
  width,
  height,
  { texture: texture }
);

// New implementation (from Chrome 150)
const sourceDict = { source: element };
const destDict = {
  destination: { texture: texture },
  width: width,
  height: height
};

GPUQueue.copyElementImageToTexture(sourceDict, destDict);

การเปลี่ยนแปลงใน Chrome 155

Chrome 155 มีการปรับแต่งเพิ่มเติมตามความคิดเห็นของนักพัฒนาซอฟต์แวร์ เรากำลังอัปเดตชื่อเมธอด กำหนดให้มีการจัดสรรหน่วยความจำพื้นผิวล่วงหน้าอย่างชัดเจน รองรับซับทรี DOM ที่ซับซ้อน และแยกการซิงค์ DOM ออกจากกัน

WebGPU

เปลี่ยนชื่อเมธอด WebGPU จาก copyElementImageToTexture เป็น drawElementImageToTexture แล้ว นอกจากการเปลี่ยนชื่อแล้ว พิกัดพารามิเตอร์ยังขยายออก (sourceX, sourceWidth) และขอบเขตปลายทางจะจัดกลุ่มเป็นออบเจ็กต์ size (GPUExtent3D) มาตรฐาน ดังนี้

// BEFORE (Chrome 150)
const sourceDict = {
  source: myElement,
  sx: 0,
  sy: 0,
  swidth: 100,
  sheight: 100
};
const destDict = {
  destination: { texture: myTexture },
  width: 300,
  height: 200
};

queue.copyElementImageToTexture(sourceDict, destDict);

// AFTER (Chrome 155+)
const sourceDict = {
  source: myElement,
  sourceX: 0,
  sourceY: 0,
  sourceWidth: 100,
  sourceHeight: 100
};
const destDict = {
  texture: myTexture,
  size: { width: 300, height: 200 }
};

queue.drawElementImageToTexture(sourceDict, destDict);

WebGL

การเปลี่ยนแปลงใน WebGL สำหรับ HTML-in-Canvas มี 3 ด้าน ดังนี้

  • ชื่อเมธอดจะเปลี่ยนจาก texElementImage2D เป็น texElementSubImage2D`
  • ลายเซ็นเมธอดมีการเปลี่ยนแปลง โดยที่สำคัญที่สุดคือมีการเพิ่มอาร์กิวเมนต์ xoffset และ yoffset เพื่อให้ระบุออฟเซ็ตในพื้นผิวปลายทางที่จะวาดองค์ประกอบได้ (ไม่บังคับ)
  • เมธอดนี้จะไม่จัดสรรพื้นที่เก็บข้อมูลบัฟเฟอร์สำหรับพื้นผิวปลายทางโดยอัตโนมัติอีกต่อไป โดยจะต้องจัดสรรพื้นที่เก็บข้อมูลอย่างชัดเจนก่อนใช้ API ซึ่งคล้ายกับลักษณะการทำงานของ texSubImage2D
// 1. Capture natural layout size of the element
const elementImage = canvas.captureElementImage(element);

// 2. Pre-allocate texture backing once (or when element resizes)
gl.bindTexture(gl.TEXTURE_2D, texture);
gl.texImage2D(
  gl.TEXTURE_2D,
  0,
  gl.RGBA8,
  Math.ceil(elementImage.width),
  Math.ceil(elementImage.height),
  0,
  gl.RGBA,
  gl.UNSIGNED_BYTE,
  null // null reserves VRAM without uploading pixels
);

// 3. Upload DOM into pre-allocated VRAM
gl.texElementSubImage2D(
  gl.TEXTURE_2D,
  0,                  // level
  0,                  // xoffset
  0,                  // yoffset
  element             // pass DOM element
);

แอตทริบิวต์ Canvas และองค์ประกอบย่อยที่วาดได้

เรากำลังอัปเดตวิธีเตรียม Canvas เพื่อแสดงผล HTML

กำลังเปลี่ยนชื่อ layoutsubtree เป็น content="drawable"

เพื่อให้สอดคล้องกับรูปแบบการตั้งชื่อแอตทริบิวต์ HTML แอตทริบิวต์บูลีน layoutsubtree ในองค์ประกอบ <canvas> จึงเปลี่ยนชื่อเป็น content="drawable" (ดูการพูดคุยโดยละเอียดได้ที่ปัญหา WICG #169)

แอตทริบิวต์ drawable สำหรับองค์ประกอบสืบทอด

ก่อนหน้านี้ ระบบจะแสดงผลเฉพาะรายการย่อยโดยตรงของ Canvas เท่านั้น เพื่อรองรับลำดับชั้น DOM ที่ซับซ้อนโดยไม่ต้องปรับมาร์กอัปให้แบนราบ ตอนนี้คุณสามารถเพิ่มแอตทริบิวต์ drawable ลงในองค์ประกอบสืบทอดที่ต้องการวาดอย่างอิสระได้อย่างชัดเจน

<!-- BEFORE -->
<canvas layoutsubtree>
  <div id="badge">
    <h2>Player Stats</h2>
    <button id="actionBtn">Equip</button>
  </div>
</canvas>

<!-- AFTER (Chrome 155+) -->
<canvas content="drawable">
  <!-- Subtree A (Body and heading) -->
  <div id="badge" drawable>
    <h2>Player Stats</h2>
    <!-- Subtree B (Excluded from Subtree A; drawn and animated independently) -->
    <button id="actionBtn" drawable>Equip</button>
  </div>
</canvas>

องค์ประกอบที่มี drawable จะจับภาพทั้งซับทรียกเว้นองค์ประกอบสืบทอดที่ซ้อนกันซึ่งระบุ drawable ด้วย ซึ่งจะช่วยให้คอนเทนเนอร์หลักและองค์ประกอบย่อย เช่น ปุ่ม สามารถเคลื่อนไหวและวาดในคำสั่งประมวลผลแยกต่างหากได้

การซิงค์การทดสอบตัวชี้

ก่อนหน้านี้ นักพัฒนาแอปต้องบันทึกค่าที่แสดงผลจากเมธอดการวาดและส่งไปยัง element.style.transform ตอนนี้เมธอด drawElementImage() จะแสดงผลเป็น void และการซิงค์จะขึ้นอยู่กับบริบท

บริบท 2 มิติ: การซิงค์ข้อมูลอัตโนมัติ

ใน Canvas 2 มิติ เบราว์เซอร์จะซิงค์ขอบเขตการทดสอบตัวชี้ DOM และวงแหวนโฟกัสของโปรแกรมอ่านหน้าจอกับพิกัดที่วาดโดยอัตโนมัติ คุณสามารถนำการไปป์การเปลี่ยนรูปแบบที่ทำด้วยตนเองทั้งหมดออกได้โดยทำดังนี้

// BEFORE
const transform = ctx.drawElementImage(element, x, y);
element.style.transform = transform.toString();

// AFTER (Chrome 155+)
ctx.drawElementImage(element, x, y);
// No style update needed: position & hit-testing sync automatically

หากต้องการจัดการเรขาคณิตด้วยตนเองในแบบ 2 มิติ ให้ส่ง { preserveElementGeometry: true } ไปยัง drawElementImage() และใช้การเปลี่ยนรูปแบบโดยใช้ canvas.updateElementGeometry()

บริบท 3 มิติ: updateElementGeometry() ที่ชัดเจน

หากต้องการซิงค์ DOM ในบริบท WebGL หรือ WebGPU ให้สร้างการเปลี่ยนรูปแบบ DOMMatrix และส่งไปยัง canvas.updateElementGeometry() ดังนี้

canvas.addEventListener('paint', () => {
  // 1. Upload element snapshot to GPU texture
  device.queue.drawElementImageToTexture({ source: element }, { texture });

  // 2. Render 3D scene...
  drawScene();

  // 3. Construct transform matrix as needed
  const canvasTransform = new DOMMatrix().translate(x, y);

  // 4. Update DOM hit-testing and accessibility bounding boxes
  canvas.updateElementGeometry(element, { canvasTransform });
});

ความเข้ากันได้ข้ามเวอร์ชัน

หากแอปพลิเคชันของคุณต้องรองรับผู้ใช้ใน Chrome หลายเวอร์ชันในช่วงระยะเวลาการเปลี่ยนผ่าน ให้ตรวจสอบว่ามีเมธอดที่อัปเดตในต้นแบบหรือไม่ และปรับการติดตั้งใช้งานตามความเหมาะสม

ตรวจสอบว่าคุณได้ตั้งค่าแอตทริบิวต์องค์ประกอบ <canvas> ที่ถูกต้องสำหรับเบราว์เซอร์ทุกเวอร์ชัน ดังนี้

// ============================================================================
//  Cross-version canvas markup
// ============================================================================
if (!canvas.hasAttribute('content') && !canvas.hasAttribute('layoutsubtree')) {
  if ('content' in HTMLCanvasElement.prototype) {
    // Chrome 155+ (New standardized attribute)
    canvas.setAttribute('content', 'drawable');
  } else {
    // Chrome < 155 (Legacy boolean attribute)
    canvas.setAttribute('layoutsubtree', '');
  }
}

สำหรับบริบท 2 มิติ ให้ตรวจสอบค่าที่แสดงผลของ drawElementImage() และตรวจสอบแอตทริบิวต์ต้นแบบของ Canvas เพื่อรองรับทั้ง 2 เวอร์ชัน ดังนี้

// ============================================================================
// Cross-version 2D drawing & synchronization
// ============================================================================
function draw2DElement(ctx, element, x, y) {
  // Execute the 2D draw call
  const result = ctx.drawElementImage(element, x, y);

  if (result !== undefined && typeof result?.toString === 'function') {
    // Chrome < 155: drawElementImage returned a DOMMatrix
    // Requires manual transform piping to align DOM hit-test bounds:
    element.style.transform = result.toString();
  } else {
    // Chrome 155+: drawElementImage returns void / undefined
    // The browser automatically aligns DOM positioning, clicks, and a11y bounds!
    // If you previously had element.style.transform logic here, clear it:
    if (element.style.transform) {
      element.style.transform = "";
    }
  }
}

สำหรับ WebGPU ให้ใช้ซิกเนเจอร์และชื่อเมธอดที่เข้ากันได้ ดังนี้

// ============================================================================
// WebGPU cross-version support
// ============================================================================
if ('drawElementImageToTexture' in GPUQueue.prototype) {
  // Chrome 155+ (Latest implementation)
  queue.drawElementImageToTexture(
    { source: element, sourceX: 0, sourceY: 0, sourceWidth: w, sourceHeight: h },
    { texture: myTexture, size: { width: w, height: h } }
  );
} else if (typeof queue.copyElementImageToTexture === "function") {
  // Chrome 150 - 154: Try the Chrome 150+ dictionary signature first,
  // then fall back to the Chrome < 150 signature
  try {
    queue.copyElementImageToTexture(
      { source: element },
      { destination: { texture: myTexture }, width: w, height: h }
    );
  } catch (err) {
    // Chrome < 150
    queue.copyElementImageToTexture(element, { texture: myTexture });
  }
}

โปรดตรวจสอบความเข้ากันได้ของเบราว์เซอร์สำหรับ WebGL ดังนี้

// ============================================================================
// WebGL cross-version support
// ============================================================================
if ('texElementSubImage2D' in WebGL2RenderingContext.prototype) {
  // Chrome 155+ (Explicit pre-allocation + sub-image)
  gl.texElementSubImage2D(gl.TEXTURE_2D, 0, 0, 0, element);
} else if (gl.texElementImage2D && gl.texElementImage2D.length === 3) {
  // Chrome 150-154 (3-argument signature)
  gl.texElementImage2D(gl.TEXTURE_2D, gl.RGBA8, element);
} else if (gl.texElementImage2D) {
  // Chrome < 150 (6-argument signature)
  gl.texElementImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, element);
}

ส่วนขยายช่วงทดลองใช้จากต้นทาง

ช่วงทดลองใช้ HTML-in-Canvas จากต้นทางได้รับการขยายเวลาไปจนถึง Chrome 160 อย่าลืมต่ออายุโทเค็นช่วงทดลองใช้จากต้นทางเพื่อนำเสนอฟีเจอร์ HTML-in-Canvas แก่ผู้ใช้ต่อไป ดูรายละเอียดเพิ่มเติมได้ที่บทความเกี่ยวกับช่วงทดลองใช้จากต้นทาง

แชร์ความคิดเห็น

โปรดแจ้งให้เราทราบว่าการอัปเดตเหล่านี้ทำงานอย่างไรในแอปพลิเคชันของคุณโดยยื่นปัญหาในที่เก็บ GitHub ของ WICG สมัครรับจดหมายข่าวสำหรับนักพัฒนาแอปเพื่อติดตามข่าวสารเกี่ยวกับ HTML-in-Canvas