原生js实现一个图片裁剪效果代码
代码语言:html
所属分类:其他
代码描述:原生js实现一个图片裁剪效果代码
下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开
<!DOCTYPE html> <html lang="en" > <head> <meta charset="UTF-8"> <style> *, *::before, *::after { font-family: inherit; box-sizing: inherit; margin: 0; padding: 0; } html { box-sizing: border-box; font-family: 'Nunito Sans', sans-serif; font-size: 62.5%; } html body { font-size: 1.6rem; margin: 0; } ul { list-style: none; } a, a:link, a:visited { text-decoration: none; } </style> <style> *, *::before, *::after { margin: 0; padding: 0; box-sizing: inherit; font-family: inherit; } html { font-size: 62.5%; overflow: hidden; } html body { font-size: 1.6rem; width: 100vw; height: 100vh; overflow: inherit; background-color: #333; display: -webkit-box; display: flex; -webkit-box-align: center; align-items: center; -webkit-box-pack: center; justify-content: center; } .image-crop { background-color: black; position: relative; overflow: hidden; } .image-crop figure { display: block; position: absolute; top: 0; left: 0; width: 100%; height: 100%; background-position: center; background-repeat: no-repeat; background-size: contain; } .image-crop figure.blur { -webkit-filter: blur(6px) brightness(0.33) saturate(0.5); filter: blur(6px) brightness(0.33) saturate(0.5); } .image-crop figure:not(.blur) { -webkit-clip-path: inset(calc(var(--top, 0) * 1px) calc(var(--right, 0) * 1px) calc(var(--bottom, 0) * 1px) calc(var(--left, 0) * 1px)); clip-path: inset(calc(var(--top, 0) * 1px) calc(var(--right, 0) * 1px) calc(var(--bottom, 0) * 1px) calc(var(--left, 0) * 1px)); } .image-crop span { display: block; position: absolute; top: calc(var(--top, 0) * 1px); right: calc(var(--right, 0) * 1px); bottom: calc(var(--bottom, 0) * 1px); left: calc(var(--left, 0) * 1px); border: dashed 2px rgba(255, 255, 255, 0.75); } .image-crop span::before, .image-crop span::after { content: ''; display: block; position: absolute; top: 50%; left: 50%; -webkit-transform: translate(-50%, -50%); transform: translate(-50%, -50%); } .image-crop span::before { width: 33.3%; height: 100%; border-left: dashed 1px rgba(255, 255, 255, 0.5); border-right: dashed 1px rgba(255, 255, 255, 0.5); } .image-crop span::after { height: 33.3%; width: 100%; border-top: dashed 1px rgba(255, 255, 255, 0.5); border-bottom: dashed 1px rgba(255, 255, 255, 0.5); } </style> </head> <body translate="no" > <div class="image-crop" data-image="//repo.bfw.wiki/bfwrepo/image/5e5ef040f1ffc.png?x-oss-process=image/auto-orient,1/resize,m_fill,w_360,h_400,/quality,q_90" style="width: 360px; height: 480px;"></div> <script > /* config */ const minWidth = 32; const minHeight = 32; const threshold = 12; (() => { const startPoint = [0, 0]; let dragging = false; const dir = [0, 0, 0, 0]; let currentElement, currentDimention, startDimention; const TOP = 0,RIGHT = 1,BOTTOM = 2,LEFT = 3; let moving = false; const onMouseMove = evt => { const { clientX: x, clientY: y } = evt.touches && evt.touches[0] || evt; if (!dragging) return; const diff = [x - startPoint[0], y - startPoint[1]]; if (moving) { diff[0] = Math.min(Math.max(diff[0], -startDimention[LEFT]), startDimention[RIGHT]); diff[1] = Math.min(Math.max(diff[1], -startDimention[TOP]), startDimention[BOTTOM]); } currentDimention[LEFT] = Math.min(Math.max(startDimention[LEFT] + dir[LEFT] * diff[0], 0), currentElement.clientWidth - currentDimention[RIGHT] - minWidth); currentDimention[RIGHT] = Math.min(Math.max(startDimention[RIGHT] + dir[RIGHT] * diff[0], 0), currentElement.clientWidth - currentDimention[LEFT] - minWidth); currentDimention[TOP] = Math.min(Math.max(startDimention[TOP] + dir[TOP] * diff[1], 0), currentElement.clientHeight - currentDimention[BOTTOM] - minHeight); currentDimention[.........完整代码请登录后点击上方下载按钮下载查看
网友评论0