css+js实现 图片手绘svg置换效果编辑器代码
代码语言:html
所属分类:其他
代码描述:css+js实现一个图片手绘svg置换效果编辑器代码,左侧鼠标绘制置换图,右侧就能看到实时的图片置换效果。
代码标签: css js 图片 手绘 svg 置换 编辑器 代码
下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开
<!DOCTYPE html> <html lang="en" > <head> <meta charset="UTF-8"> <style> /* Reset and Basic Styles */ * { box-sizing: border-box; } body { margin: 0; padding: 20px; background-color: #1e1e1e; color: #ffffff; font-family: Arial, sans-serif; } h2 { text-align: center; margin-bottom: 10px; } /* Container for both panels */ .container { display: flex; flex-wrap: wrap; gap: 20px; justify-content: center; } /* Panel Styles */ .panel { background-color: #2e2e2e; padding: 20px; border-radius: 10px; flex: 1 1 500px; max-width: 600px; } .left-panel, .right-panel { display: flex; flex-direction: column; align-items: center; } /* Canvas Styles */ #drawing-canvas { border: 2px solid #555; border-radius: 10px; background-color: #808080; /* Default gray background */ cursor: crosshair; } /* Preview Image Styles */ .preview-container { width: 500px; height: 500px; overflow: hidden; border: 2px solid #555; border-radius: 10px; background-color: #000; } #preview-image { width: 100%; height: 100%; object-fit: cover; filter: url(#displacementFilter); } /* Controls Styles */ .controls { margin-top: 20px; width: 100%; } .control-group { margin-bottom: 15px; display: flex; align-items: center; flex-wrap: wrap; } .control-group label { margin-right: 10px; flex: 1 1 150px; } .control-group input[type="range"] { flex: 2 1 200px; } .color-btn, .noise-btn { width: 30px; height: 30px; border: none; margin-right: 5px; cursor: pointer; border-radius: 5px; } .color-btn.active, .noise-btn.active { border: 2px solid #ffffff; } #custom-color { width: 40px; height: 30px; border: none; cursor: pointer; } .noise-btn { width: auto; padding: 5px 10px; margin-right: 5px; background-color: #555; color: #fff; border-radius: 5px; } .noise-btn:hover, .noise-btn.active { background-color: #777; } /* Button Styles */ button { padding: 10px 20px; background-color: #555; border: none; border-radius: 5px; color: #fff; cursor: pointer; transition: background-color 0.3s; margin-right: 10px; } button:hover { background-color: #777; } button:active { background-color: #999; } /* Responsive Adjustments */ @media (max-width: 1200px) { .preview-container, #drawing-canvas { width: 400px; height: 400px; } } @media (max-width: 800px) { .preview-container, #drawing-canvas { width: 300px; height: 300px; } } </style> </head> <body> <div class="container"> <!-- Left Panel: Drawing Canvas and Controls --> <div class="panel left-panel"> <h2>Create Displacement Map</h2> <canvas id="drawing-canvas" width="500" height="500"></canvas> <div class="controls"> <!-- Brush Controls --> <div class="control-group"> <label>Brush Color:</label> <button class="color-btn" data-color="#FF0000" style="background-color: #FF0000;"></button> <button class="color-btn" data-color="#00FF00" style="background-color: #00FF00;"></button> <button class="color-btn" data-color="#0000FF" style="background-color: #0000FF;"></button> <button class="color-btn" data-color="#808080" style="background-color: #808080;"></button> <input type="color" id="custom-color" value="#FFFFFF" title="Custom Color"> </div> <div class="control-group"> <label for="brush-size">Brush Size: <span id="brush-size-value">10</span>px</label> <input type="range" id="brush-size" min="1" max="50" value="10"> </div> <div class="control-group"> <label for="brush-opacity">Opacity: <span id="brush-opacity-value">1.0</span></label> <input type="range" id="brush-opacity" min="0.1" max="1" step="0.1" value="1"> </div> <!-- Noise Generation Controls --> <div class="control-group"> <label>Generate Noise:</label> <button class="noise-btn" data-noise="fractal">Fractal Noise</button> <button class="noise-btn" data-noise="turbulence">Turbulence</button> <button class="noise-btn" data-noise="stripes">Stripes</button> <button class="noise-btn" data-noise="checkerboard">Checkerboard</button> </div> <!-- Overlay Control --> <div class="control-group"> <label for="overlay-toggle">Overlay with Source Image:</label> <input type="checkbox" id="overlay-toggle"> </div> <!-- Action Buttons --> <div class="control-group"> <button id="reset-btn">Reset Canvas</button> <button id="download-btn">Download Displacement Map</button> </div> </div> </div> <!-- Right Panel: Displacement Effect Preview and Controls --> <div class="panel right-panel"> <h2>Displacement Effect Preview</h2> <div class="preview-container"> <img id="preview-image" src="//repo.bfw.wiki/bfwrepo/image/65c5688cec812.png" alt="Preview Image"> </div> <div class="controls"> <div class="control-group"> <label for="displacement-scale">Displacement Scale: <span id="displacement-scale-value">20</span></label> <input type="range" id="displacement-scale" min="0" max="100" value="20"> </div> </div> </div> </div> <!-- SVG Filter Definition --> <svg xmlns="http://www.w3.org/2000/svg" style="display: none;"> <defs> <filter id="displacementFilter"> <!-- feImage will be updated with the displacement map --> <feImage id="displacement-map" xlink:href="" result="displacementMap" /> <feDisplacementMap in="SourceGraphic" in2="displacementMap" scale="20" xChannelSelector="R" yChannelSelector="G" /> </filter> </defs> </svg> <script > document.addEventListener('DOMContentLoaded', () => { // Canvas Setup const canvas = document.getElementById('drawing-canvas'); const ctx = canvas.getContext('2d'); // Initialize canvas with default gray background function initializeCanvas() { ctx.fillStyle = '#808080'; ctx.fillRect(0, 0, canvas.width, canvas.height); } initializeCanvas(); // Drawing State let drawing = false; let brushColor = '#FFFFFF'; let brushSize = 10; let brushOpacity = 1.0; // Set up brush controls const colorButtons = document.querySelectorAll('.color-btn'); const customColorPicker = document.getElementById('custom-color'); const brushSizeSlider = document.getElementById('brush-size'); const brushSizeValue = document.getElementById('brush-size-value'); const brushOpacitySlider = document.getElementById('brush-opacity'); const brushOpacityValue = document.getElementById('brush-opacity-value'); const downloadBtn = document.getElementById('download-btn'); const resetBtn = document.getElementById('reset-btn'); const noiseButtons = document.querySelectorAll('.noise-btn'); const overlayToggle = document.getElementById('overlay-toggle'); // Handle color button clicks colorButtons.forEach(btn => { btn.addEve.........完整代码请登录后点击上方下载按钮下载查看
网友评论0