js实现svg动画逐帧导出图片代码

代码语言:html

所属分类:其他

代码描述:js实现svg动画逐帧导出图片代码

代码标签: js svg 动画 逐帧 导出 图片 代码

下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开

<!DOCTYPE html>
<html lang="en" >

<head>
  <meta charset="UTF-8">
  
  <meta name="viewport" content="width=device-width, initial-scale=1">
  
  
  
<style>
*,
*::before,
*::after {
	box-sizing: border-box;
	margin: 0;
	padding: 0;
}

body {
	font-family: "Segoe UI", Tahoma, sans-serif;
	background: #f0f2f5;
	color: #333;
	line-height: 1.5;
	padding: 1rem;
}

h1 {
	text-align: center;
	margin-bottom: 1rem;
	font-size: 2rem;
	color: #444;
}

.container {
	max-width: 1000px;
	margin: 0 auto;
	display: grid;
	grid-template-columns: 1fr;
	gap: 1.5rem;
}

@media (min-width: 768px) {
	.container {
		grid-template-columns: 1fr 1fr;
	}
}

.card {
	background: #fff;
	border-radius: 8px;
	box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
	padding: 1rem;
	overflow: hidden;
}

.controls {
	display: flex;
	flex-direction: column;
	gap: 0.75rem;
}

.controls label {
	display: flex;
	flex-direction: column;
	font-weight: 500;
}

.controls label span {
	margin-bottom: 0.25rem;
}

.controls input[type="number"],
.controls button,
.controls input[type="color"],
.controls textarea,
.controls input[type="file"] {
	font-family: inherit;
}

button {
	background-color: #007bff;
	color: #fff;
	border: none;
	padding: 0.6rem 1rem;
	border-radius: 4px;
	cursor: pointer;
	font-size: 1rem;
	transition: background 0.2s ease;
}

button:hover {
	background-color: #0056b3;
}

textarea {
	width: 100%;
	min-height: 80px;
	border: 1px solid #ccc;
	border-radius: 4px;
	padding: 0.5rem;
	resize: vertical;
}

input[type="file"] {
	padding: 0.3rem;
}

#mysvg {
	width: 100%;
	max-width: 100%;
	height: auto;
	border: 1px solid #ddd;
	border-radius: 4px;
	background: #fff;
}

#preview {
	display: block;
	margin: 0.5rem auto;
	border: 1px solid #ddd;
	border-radius: 4px;
}

/* Carousel stays within card width */
.carousel-container {
	display: flex;
	flex-wrap: nowrap;
	overflow-x: auto;
	overflow-y: hidden;
	width: 100%;
	padding: 0.5rem 0;
}

.carousel-container::-webkit-scrollbar {
	height: 8px;
}

.carousel-container::-webkit-scrollbar-thumb {
	background: rgba(0, 0, 0, 0.2);
	border-radius: 4px;
}

.carousel-item {
	flex: 0 0 auto;
	position: relative;
	margin-right: 0.5rem;
}

.frame-img {
	display: block;
	border-radius: 4px;
}

.badge {
	position: absolute;
	background: rgba(0, 0, 0, 0.6);
	color: #fff;
	padding: 2px 6px;
	border-radius: 3px;
	font-size: 0.75rem;
}

.badge.frame {
	top: 5px;
	left: 5px;
}

.badge.time {
	bottom: 5px;
	right: 5px;
}

.status {
	text-align: center;
	margin-top: 0.5rem;
	font-weight: bold;
}

.loading {
	text-align: center;
	display: none;
}
</style>


  
  
</head>

<body translate="no">
  <h1>Animated SVG to PNG Frames</h1>
<div class="container">
	<!-- SVG Display -->
	<div class="card">
		<svg id="mysvg" viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg">
			<g id="coolBean">
				<g class="bean">
					<path fill="brown" stroke="#fff" stroke-width="2" d="M 17 59 c -13 8 -13 39 15 35 a 50 50 0 0 0 31 -83 c -11 -11 -38 -4 -25 29 c 5 14 -9 11 -21 19z" />
					<path fill="rgba(255,255,255,0.5)" d="M 42 42 c 7 7 -2 16 -12 14 c 6 -2 14 -5 12 -14 z" />
				</g>
				<g class="glasses" transform="translate(0 -42)">
					<path fill="#000" d="M 10 24 h 90 v 7.5 h -4 v 3.5 h -3.5 v 3 h -3.5 v 3.5 h -24 v -3.5 h -3.5 v -3 h -3.5 v -7 h -6 v 7 h -3.5 v 3 h -3.5 v 3.5 h -24.5 v -3.5 h -3.5 v -3 h -3.5 v -3.5 h -3.5 z" />
					<path fill="#fff" d="M 17 28 h 3.5 v 3.5 h -3.5 z m 3.5 3.5 h 3.5 v 3.5 h -3.5 z m 3.5 3.5 h 3.5 v 3.5 h -3.5 z m 0 -7 h 3.5 v 3.5 h -3.5 z m 3.5 3.5 h 3.5 v 3.5 h -3.5 z m 3.5 3.5 h 3.5 v 3.5 h -3.5 z m 31 -7 h 3.5 v 3.5 h -3.5 z m 3.5 3.5 h 3.5 v 3.5 h -3.5 z m 3.5 3.5 h 3.5 v 3.5 h -3.5 z m 0 -7 h 3.5 v 3.5 h -3.5 z m 3.5 3.5 h 3.5 v 3.5 h -3.5 z m 3.5 3.5 h 3.5 v 3.5 h -3.5 z" />
					<animateTransform attributeName="transform" type="translate" values="0 -42; 0 0" dur="3s" begin="0s" repeatCount="0" fill="freeze" />
				</g>
			</g>
		</svg>
	</div>

	<!-- Controls -->
	<div class="card controls">
		<h2>Controls</h2>
		<label>
			<span>Use Example SVG</span>
			<button id="useExampleSvg">Load Example</button>
		</label>
		<label>
			<span>Paste SVG String</span>
			<textarea id="svgTextInput" placeholder="Paste raw SVG markup here..."></textarea>
			<button id="loadTextSvg">Load SVG</button>
		</label>
		<label>
			<span>Import SVG File</span>
			<input type="file" id="importSvgInput" accept=".svg">
		</label>
		<br/>
		<label>
			<span title="The value of the smallest of either the picture's height or width. Example: a 20 x 80 svg image will be scaled up to 200 x 800">Minimum pixel dimensions</span>
			<input type="number" id="pixelMinimumInput" value="200" step="10" min="100" max="1000">
		</label>
	</div>

	<!-- Single Frame Export -->
	<div class="card controls">
		<h2>Export Frame</h2>
		<label>
			<span>Background</span>
			<div style="display:flex; align-items:center; gap:0.5rem;">
				<input type="checkbox" id="singleTransparent">
				<label for="singleTransparent" style="font-weight:400; margin:0;">Transparent</label>
				<input type="color" id="singleBgColor" value="#ffffff">
			</div>
		</label>
		<label>
			<span>Freeze at (seconds)</span>
			<input type="number" id="timeInput" value="1" step="0.1" min="0" max="3">
		</label>
		<button id="export">Export as PNG</button>
		<img id=&qu.........完整代码请登录后点击上方下载按钮下载查看

网友评论0