当前位置:首页 > JavaScript

用JS实现冰墩墩

2026-01-31 09:16:43JavaScript

用JS实现冰墩墩

以下是用JavaScript绘制冰墩墩的几种实现方法,结合Canvas绘图和SVG路径数据:

使用Canvas绘制基础轮廓

const canvas = document.createElement('canvas');
canvas.width = 400;
canvas.height = 400;
document.body.appendChild(canvas);
const ctx = canvas.getContext('2d');

// 绘制头部轮廓
ctx.beginPath();
ctx.arc(200, 150, 100, 0, Math.PI * 2);
ctx.fillStyle = '#ffffff';
ctx.fill();
ctx.strokeStyle = '#000000';
ctx.lineWidth = 2;
ctx.stroke();

// 绘制黑色眼圈
ctx.beginPath();
ctx.ellipse(160, 130, 30, 40, 0, 0, Math.PI * 2);
ctx.ellipse(240, 130, 30, 40, 0, 0, Math.PI * 2);
ctx.fillStyle = '#000000';
ctx.fill();

// 绘制彩色光环
const gradient = ctx.createLinearGradient(100, 50, 300, 50);
gradient.addColorStop(0, '#ff3366');
gradient.addColorStop(0.5, '#33ccff');
gradient.addColorStop(1, '#ffcc33');
ctx.beginPath();
ctx.arc(200, 150, 110, 0, Math.PI * 2);
ctx.lineWidth = 8;
ctx.strokeStyle = gradient;
ctx.stroke();

使用SVG路径数据精确绘制

const svgNS = "http://www.w3.org/2000/svg";
const svg = document.createElementNS(svgNS, "svg");
svg.setAttribute("width", "400");
svg.setAttribute("height", "400");
document.body.appendChild(svg);

// 身体轮廓路径
const bodyPath = document.createElementNS(svgNS, "path");
bodyPath.setAttribute("d", "M200,250 C280,250 320,200 320,150 C320,80 260,50 200,50 C140,50 80,80 80,150 C80,200 120,250 200,250");
bodyPath.setAttribute("fill", "white");
bodyPath.setAttribute("stroke", "black");
svg.appendChild(bodyPath);

// 添加奥运五环
const colors = ['#0085C7', '#000000', '#F4C300', '#009F3D', '#DF0024'];
for (let i = 0; i < 5; i++) {
    const ring = document.createElementNS(svgNS, "circle");
    ring.setAttribute("cx", 150 + i * 25);
    ring.setAttribute("cy", 180);
    ring.setAttribute("r", 10);
    ring.setAttribute("fill", "none");
    ring.setAttribute("stroke", colors[i]);
    ring.setAttribute("stroke-width", "2");
    svg.appendChild(ring);
}

使用CSS绘制简化版

<style>
.bing-dwen-dwen {
    position: relative;
    width: 200px;
    height: 250px;
    background: white;
    border-radius: 50%;
    border: 2px solid black;
}
.eyes {
    position: absolute;
    width: 60px;
    height: 80px;
    background: black;
    border-radius: 50%;
}
.left-eye { top: 30%; left: 20%; }
.right-eye { top: 30%; right: 20%; }
.rainbow {
    position: absolute;
    width: 220px;
    height: 220px;
    border-radius: 50%;
    border: 8px solid transparent;
    border-image: linear-gradient(to right, red, orange, yellow, green, blue);
    border-image-slice: 1;
    top: -10px;
    left: -10px;
}
</style>

<div class="bing-dwen-dwen">
    <div class="rainbow"></div>
    <div class="eyes left-eye"></div>
    <div class="eyes right-eye"></div>
</div>

注意事项

  • 精确还原需要参考官方设计图纸的路径数据
  • 彩色渐变部分建议使用HSL颜色空间实现平滑过渡
  • 三维效果可通过添加阴影和光照渐变增强
  • 动画效果可使用CSS关键帧或requestAnimationFrame实现

以上代码提供了不同精度的实现方案,可根据需求选择合适的方式。完整实现建议结合矢量图形工具导出的精确路径数据。

用JS实现冰墩墩

标签: JS冰墩墩
分享给朋友:

相关文章

JS实现哀悼

JS实现哀悼

实现网页哀悼效果的方法 通过CSS滤镜和全局样式调整,可以快速实现网页整体变灰的哀悼效果。以下是具体实现方式: document.addEventListener('DOMContentLo…

JS如何调用react组件

JS如何调用react组件

调用React组件的常见方法 在JavaScript中调用React组件通常涉及以下几种场景和方式: 直接渲染组件 通过ReactDOM.render()方法将组件渲染到DOM节点: import…

JS如何访问react内部的数据

JS如何访问react内部的数据

访问 React 组件内部数据的方法 在 React 中,组件内部的数据通常通过 state 或 props 管理。以下是几种常见的访问方式: 通过 state 访问数据 React 组件的内部状…

JS如何实现左右滑动

JS如何实现左右滑动

实现左右滑动的方法 使用 touchstart、touchmove 和 touchend 事件监听触摸操作,计算滑动距离和方向。 let startX, moveX; element.addEve…

JS实现日期滚动选择

JS实现日期滚动选择

实现日期滚动选择的基本思路 使用HTML、CSS和JavaScript创建一个日期滚动选择器,允许用户通过滚动选择年、月、日。核心是通过监听滚动事件,动态更新显示的值。 HTML结构 创建一个包含年…

JS实现文本的删除

JS实现文本的删除

使用 substring() 方法 通过指定起始和结束索引截取字符串的一部分,间接实现删除效果。 let str = "Hello World"; let newStr = str.substr…