当前位置:首页 > JavaScript

js实现动态画圆

2026-02-03 06:20:49JavaScript

使用Canvas绘制动态圆

在HTML中创建Canvas元素并获取其上下文:

<canvas id="myCanvas" width="400" height="400"></canvas>

JavaScript代码实现动态画圆:

const canvas = document.getElementById('myCanvas');
const ctx = canvas.getContext('2d');

let radius = 0;
const maxRadius = 100;
const centerX = canvas.width / 2;
const centerY = canvas.height / 2;

function drawCircle() {
    ctx.clearRect(0, 0, canvas.width, canvas.height);
    ctx.beginPath();
    ctx.arc(centerX, centerY, radius, 0, Math.PI * 2);
    ctx.strokeStyle = 'blue';
    ctx.lineWidth = 3;
    ctx.stroke();

    radius += 1;
    if (radius <= maxRadius) {
        requestAnimationFrame(drawCircle);
    }
}

drawCircle();

使用SVG实现动态圆

HTML中创建SVG容器:

<svg id="svgContainer" width="400" height="400"></svg>

JavaScript动态创建和动画SVG圆:

js实现动态画圆

const svg = document.getElementById('svgContainer');
const circle = document.createElementNS("http://www.w3.org/2000/svg", "circle");

let currentRadius = 0;
const maxRadius = 100;
const centerX = 200;
const centerY = 200;

circle.setAttribute('cx', centerX);
circle.setAttribute('cy', centerY);
circle.setAttribute('r', currentRadius);
circle.setAttribute('stroke', 'red');
circle.setAttribute('fill', 'none');
circle.setAttribute('stroke-width', '3');

svg.appendChild(circle);

function animateSVGCircle() {
    currentRadius += 1;
    circle.setAttribute('r', currentRadius);

    if (currentRadius < maxRadius) {
        requestAnimationFrame(animateSVGCircle);
    }
}

animateSVGCircle();

使用CSS动画结合JavaScript

HTML结构:

<div id="cssCircle"></div>

CSS样式:

js实现动态画圆

#cssCircle {
    width: 0;
    height: 0;
    border-radius: 50%;
    border: 3px solid green;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
}

JavaScript控制动画:

const circle = document.getElementById('cssCircle');
let size = 0;
const maxSize = 200;

function growCircle() {
    size += 2;
    circle.style.width = size + 'px';
    circle.style.height = size + 'px';

    if (size < maxSize) {
        requestAnimationFrame(growCircle);
    }
}

growCircle();

使用GSAP库实现高级动画

引入GSAP库后:

gsap.to("#gsapCircle", {
    duration: 2,
    attr: {
        r: 100
    },
    ease: "bounce.out"
});

HTML结构:

<svg width="400" height="400">
    <circle id="gsapCircle" cx="200" cy="200" r="0" stroke="purple" stroke-width="3" fill="none"/>
</svg>

标签: 动态js
分享给朋友:

相关文章

js实现图片上传

js实现图片上传

图片上传的基本实现 使用HTML的<input type="file">元素配合JavaScript的File API可以实现图片上传功能。HTML部分需要创建一个文件选择输入框和一个用于…

js分组实现

js分组实现

分组实现方法 在JavaScript中,可以通过多种方式实现数组或对象的分组操作。以下是几种常见的方法: 使用Array.prototype.reduce() 通过reduce方法可以轻松实现数组分…

vue 实现动态样式

vue 实现动态样式

动态样式绑定方法 在Vue中实现动态样式主要通过v-bind:class和v-bind:style指令完成,以下是具体实现方式: 类名绑定 <div :class="{ active: is…

js实现密码

js实现密码

密码强度验证 使用正则表达式验证密码强度是一种常见方法。以下代码检查密码是否包含大小写字母、数字和特殊字符,且长度至少为8位: function checkPasswordStrength(pass…

js实现文字滚动

js实现文字滚动

实现文字滚动的几种方法 使用CSS动画实现滚动 通过CSS的@keyframes和transform属性可以实现平滑的文字滚动效果。 <style> .scroll-text { w…

js实现搜索

js实现搜索

实现搜索功能的方法 在JavaScript中实现搜索功能可以通过多种方式完成,以下是几种常见的实现方法。 使用数组的filter方法 通过数组的filter方法可以筛选出符合条件的数据项。假设有一个…