مثلث Canvas HTML
- الصفحة السابقة شكل Canvas
- الصفحة التالية دائرة Canvas
مثال
رسم مستطيل بحجم 150*100 بكسل:
const canvas = document.getElementById("myCanvas"); const ctx = canvas.getContext("2d"); ctx.beginPath(); ctx.rect(20, 20, 150, 100); ctx.stroke();
طريقة "rect()"
يضيف طريقة "rect()" مستطيلاً إلى المسار.
النحو:
context.rect(x, y, width, height)
المواصفات | الوصف |
---|---|
x | موقع x العلوية اليسرى للمستطيل. |
y | موقع y العلوية اليسرى للمستطيل. |
width | عرض المستطيل، بالبكسل. |
height | ارتفاع المستطيل، بالبكسل. |
مزيد من الأمثلة
مثال
استخدام طريقة "rect()" لإنشاء ثلاثة مستطيلات:
JavaScript:
const canvas = document.getElementById("myCanvas"); const ctx = canvas.getContext("2d"); // 红色矩形 ctx.beginPath(); ctx.lineWidth = "6"; ctx.strokeStyle = "red"; ctx.rect(5, 5, 290, 140); ctx.stroke(); // 绿色矩形 ctx.beginPath(); ctx.lineWidth = "4"; ctx.strokeStyle = "green"; ctx.rect(30, 30, 50, 50); ctx.stroke(); // 蓝色矩形 ctx.beginPath(); ctx.lineWidth = "10"; ctx.strokeStyle = "blue"; ctx.rect(50, 50, 150, 80); ctx.stroke();
يرجى الرجوع إلى:
- الصفحة السابقة شكل Canvas
- الصفحة التالية دائرة Canvas