Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> JavaScript

วิธีสร้างกราฟิก SVG ด้วย JavaScript

เบราว์เซอร์สมัยใหม่รองรับ SVG ทั้งหมด สามารถสร้าง SVG ด้วย JavaScript โดย:

  1. สร้าง SVG document object model (DOM) ว่างเปล่า
  2. ใช้ attributes สร้างรูปทรง เช่น วงกลม สี่เหลี่ยม
var mySvg = "https://www.w3.org/2000/svg";
var myDoc = evt.target.ownerDocument;
var myShape = svgDocument.createElementNS(mySvg, "circle");
myShape.setAttributeNS(null, "cx", 40);
myShape.setAttributeNS(null, "cy", 40);
myShape.setAttributeNS(null, "r", 30);
myShape.setAttributeNS(null, "fill", "yellow");