当前位置:首页 > JavaScript

js实现画圆

2026-02-02 03:18:03JavaScript

使用Canvas API画圆

Canvas API是HTML5提供的绘图工具,通过JavaScript调用可以绘制各种图形,包括圆形。

// 获取Canvas元素和上下文
const canvas = document.getElementById('myCanvas');
const ctx = canvas.getContext('2d');

// 绘制圆形
ctx.beginPath();
ctx.arc(100, 100, 50, 0, Math.PI * 2); // x, y, 半径, 起始角, 结束角
ctx.fillStyle = 'blue'; // 填充颜色
ctx.fill(); // 填充圆形
ctx.strokeStyle = 'black'; // 边框颜色
ctx.lineWidth = 2; // 边框宽度
ctx.stroke(); // 绘制边框

使用SVG画圆

SVG是一种矢量图形格式,可以直接在HTML中使用,也可以通过JavaScript动态创建。

js实现画圆

// 创建SVG元素
const svg = document.createElementNS("http://www.w3.org/2000/svg", "svg");
svg.setAttribute("width", "200");
svg.setAttribute("height", "200");

// 创建圆形元素
const circle = document.createElementNS("http://www.w3.org/2000/svg", "circle");
circle.setAttribute("cx", "100");
circle.setAttribute("cy", "100");
circle.setAttribute("r", "50");
circle.setAttribute("fill", "red");
circle.setAttribute("stroke", "black");
circle.setAttribute("stroke-width", "2");

// 添加到SVG和DOM中
svg.appendChild(circle);
document.body.appendChild(svg);

使用CSS画圆

CSS可以通过border-radius属性创建圆形元素,结合JavaScript可以动态控制样式。

// 创建div元素
const circleDiv = document.createElement('div');
circleDiv.style.width = '100px';
circleDiv.style.height = '100px';
circleDiv.style.borderRadius = '50%';
circleDiv.style.backgroundColor = 'green';
circleDiv.style.border = '2px solid black';

// 添加到DOM中
document.body.appendChild(circleDiv);

使用D3.js画圆

D3.js是一个强大的数据可视化库,可以方便地绘制各种图形。

js实现画圆

// 创建SVG容器
const svg = d3.select("body").append("svg")
    .attr("width", 200)
    .attr("height", 200);

// 绘制圆形
svg.append("circle")
    .attr("cx", 100)
    .attr("cy", 100)
    .attr("r", 50)
    .attr("fill", "purple")
    .attr("stroke", "black")
    .attr("stroke-width", 2);

使用Three.js画3D圆

Three.js是一个3D图形库,可以创建3D圆形或球体。

// 创建场景、相机和渲染器
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
const renderer = new THREE.WebGLRenderer();
renderer.setSize(200, 200);
document.body.appendChild(renderer.domElement);

// 创建球体几何体
const geometry = new THREE.SphereGeometry(5, 32, 32);
const material = new THREE.MeshBasicMaterial({ color: 0xffff00 });
const sphere = new THREE.Mesh(geometry, material);
scene.add(sphere);

camera.position.z = 10;

// 渲染场景
function animate() {
    requestAnimationFrame(animate);
    sphere.rotation.x += 0.01;
    sphere.rotation.y += 0.01;
    renderer.render(scene, camera);
}
animate();

使用P5.js画圆

P5.js是一个创意编程库,简化了绘图过程。

function setup() {
    createCanvas(200, 200);
}

function draw() {
    background(220);
    fill(255, 0, 0);
    stroke(0);
    strokeWeight(2);
    ellipse(100, 100, 100, 100);
}

标签: js
分享给朋友:

相关文章

js 实现继承

js 实现继承

原型链继承 通过让子类的原型对象指向父类的实例来实现继承。子类实例可以访问父类原型上的属性和方法。 function Parent() { this.name = 'parent'; } Par…

js实现复制功能

js实现复制功能

使用 document.execCommand 方法 这种方法适用于较旧的浏览器,但在现代浏览器中可能被逐步淘汰。通过创建一个临时的 textarea 元素,将文本内容放入其中,然后执行复制命令。…

js实现预览

js实现预览

文件上传预览实现 使用JavaScript实现文件上传预览功能,可以通过FileReader对象读取文件内容并显示预览。以下代码支持图片、文本和PDF文件的预览: // HTML部分需要包含…

js实现选题

js实现选题

实现选题功能的JavaScript方法 基础实现方案 使用数组存储选项,通过随机索引选取: const options = ['选项A', '选项B', '选项C', '选项D']; const r…

js实现滑动

js实现滑动

实现滑动效果的方法 在JavaScript中实现滑动效果可以通过多种方式完成,以下是几种常见的实现方法: 使用CSS过渡和JavaScript触发 通过CSS定义过渡效果,JavaScript控制触…

js实现定位

js实现定位

使用Geolocation API获取当前位置 在JavaScript中,可以通过浏览器内置的Geolocation API获取用户的地理位置信息。该API需要用户授权才能访问位置数据。 if (n…