大网数据广告活动
Web 标准与前端开发

DOMQuad: fromRect() static method

· 52CE 我爱测

The **`fromRect()`** static method of the DOMQuad interface returns a new `DOMQuad` object based on the provided set of coordinates in the shape of a DOMRect object.

Geometry Interfaces

The fromRect() static method of the DOMQuad interface returns a new DOMQuad object based on the provided set of coordinates in the shape of a DOMRect object.

Syntax

DOMQuad.fromRect()
DOMQuad.fromRect(rect)

Parameters

  • rect
    • : A DOMRect, DOMRectReadOnly, or an object with the same properties. All properties default to 0. The properties are:
      • DOMRect/x
        • : The x coordinate of the rectangle's origin (top-left corner).
      • DOMRect/y
        • : The y coordinate of the rectangle's origin (top-left corner).
      • DOMRect/width
        • : The width of the rectangle.
      • DOMRect/height
        • : The height of the rectangle.

Return value

A DOMQuad object.

Examples

Creating a rectangular quad

This example creates a DOMQuad from scratch that happens to be rectangular. Using fromRect() is more convenient than using the DOMQuad.DOMQuad constructor.

const quad = DOMQuad.fromRect({ x: 10, y: 20, width: 100, height: 50 });

console.log(quad.p1); // DOMPoint {x: 10, y: 20, z: 0, w: 1}
console.log(quad.p2); // DOMPoint {x: 110, y: 20, z: 0, w: 1}
console.log(quad.p3); // DOMPoint {x: 110, y: 70, z: 0, w: 1}
console.log(quad.p4); // DOMPoint {x: 10, y: 70, z: 0, w: 1}

Creating a quad from DOMRect

This example shows how to create a DOMQuad from a DOMRect object.

const domRect = new DOMRect(50, 60, 200, 100);
const quad = DOMQuad.fromRect(domRect);

console.log(quad.p1); // DOMPoint {x: 50, y: 60, z: 0, w: 1}
console.log(quad.p2); // DOMPoint {x: 250, y: 60, z: 0, w: 1}
console.log(quad.p3); // DOMPoint {x: 250, y: 160, z: 0, w: 1}
console.log(quad.p4); // DOMPoint {x: 50, y: 160, z: 0, w: 1}

Specifications

Browser compatibility

See also

  • DOMQuad.DOMQuad constructor
  • DOMRect
  • DOMPoint

来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
MDN Web Docs / Mozilla 贡献者
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 2.5

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。