当前位置:首页 > 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实现vue

js实现vue

Vue.js 简介 Vue.js 是一个渐进式 JavaScript 框架,用于构建用户界面。其核心库专注于视图层,易于与其他库或现有项目整合。 实现 Vue.js 的基本步骤 安装 Vue.j…

js实现轮播

js实现轮播

实现轮播图的基本思路 轮播图的核心逻辑是通过定时切换展示的图片或内容,通常结合左右滑动按钮和指示器(小圆点)增强交互性。以下是基于原生JavaScript的实现方法。 HTML结构 构建轮播图的HT…

js实现图片预览

js实现图片预览

使用FileReader API实现图片预览 通过FileReader对象读取用户选择的图片文件并显示预览: const input = document.getElementById('image…

js 实现vue

js 实现vue

Vue.js 的基本实现 在 JavaScript 中实现 Vue.js 的核心功能,可以通过数据绑定、响应式系统和虚拟 DOM 来实现。以下是实现 Vue.js 核心功能的简化版本。 数据响应式系…

js实现pdf在线预览

js实现pdf在线预览

使用PDF.js实现PDF在线预览 PDF.js是由Mozilla开发的一个开源JavaScript库,可以在网页中直接渲染PDF文件。以下是实现PDF在线预览的步骤: 引入PDF.js库 在HT…

js实现菜单

js实现菜单

实现基本HTML结构 使用HTML创建菜单的基本框架,通常包含<ul>和<li>元素。示例结构如下: <ul id="menu"> <li><…