剪裁一个带遮罩的裁剪图像

huangapple go评论109阅读模式
英文:

Cropping a masked clipped image

问题

我已创建了一个SVG遮罩,允许显示图像的特定部分。以下是相关的代码:

  1. <!-- language: lang-css -->
  2. .svg-defs {
  3. position: absolute;
  4. width: 0;
  5. height: 0;
  6. }
  7. .container {
  8. position: relative;
  9. top: 20px;
  10. left: 50px;
  11. }
  12. .background::before {
  13. content: '';
  14. position: absolute;
  15. top: 0;
  16. right: 0;
  17. bottom: 0;
  18. left: 0;
  19. background: url(https://fastly.picsum.photos/id/553/200/300.jpg?hmac=-A3VLW_dBmwUaXOe7bHhCt-lnmROrPFyTLslwNHVH1A) no-repeat;
  20. opacity: .1;
  21. }
  22. img {
  23. clip-path: url(#clipper);
  24. }
  25. <!-- language: lang-html -->
  26. <svg class='svg-defs'>
  27. <defs>
  28. <clipPath id='clipper'>
  29. <rect x="30" y="120" width="100" height="100" />
  30. </clipPath>
  31. </defs>
  32. </svg>
  33. <div class='container'>
  34. <div class='background'>
  35. <img src="https://fastly.picsum.photos/id/553/200/300.jpg?hmac=-A3VLW_dBmwUaXOe7bHhCt-lnmROrPFyTLslwNHVH1A" />
  36. </div>
  37. </div>

请注意,我在CSS中使用.background::before仅用于美观地显示图像作为淡化的背景。

至于您的要求,您想要编写JavaScript代码,当执行时(可能通过单击按钮触发),它将裁剪图像仅为剪切的部分(或上面图像中的亮部分),并且您应该能够最终将其保存到文件中(换句话说,您只想保存图像的亮部分作为新图像,或者至少能够在单独的img标签中显示它)。

英文:

I have created an SVG mask that allows showing only a certain part of an image. Here is the code for that:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

  1. .svg-defs {
  2. position: absolute;
  3. width: 0;
  4. height: 0;
  5. }
  6. .container {
  7. position: relative;
  8. top: 20px;
  9. left: 50px;
  10. }
  11. .background::before {
  12. content: &#39;&#39;;
  13. position: absolute;
  14. top: 0;
  15. right: 0;
  16. bottom: 0;
  17. left: 0;
  18. background: url(https://fastly.picsum.photos/id/553/200/300.jpg?hmac=-A3VLW_dBmwUaXOe7bHhCt-lnmROrPFyTLslwNHVH1A) no-repeat;
  19. opacity: .1;
  20. }
  21. img {
  22. clip-path: url(#clipper);
  23. }

<!-- language: lang-html -->

  1. &lt;svg class=&#39;svg-defs&#39;&gt;
  2. &lt;defs&gt;
  3. &lt;clipPath id=&#39;clipper&#39;&gt;
  4. &lt;rect x=&quot;30&quot; y=&quot;120&quot; width=&quot;100&quot; height=&quot;100&quot; /&gt;
  5. &lt;/clipPath&gt;
  6. &lt;/defs&gt;
  7. &lt;/svg&gt;
  8. &lt;div class=&#39;container&#39;&gt;
  9. &lt;div class=&#39;background&#39;&gt;
  10. &lt;img src=&quot;https://fastly.picsum.photos/id/553/200/300.jpg?hmac=-A3VLW_dBmwUaXOe7bHhCt-lnmROrPFyTLslwNHVH1A&quot; /&gt;
  11. &lt;/div&gt;
  12. &lt;/div&gt;

<!-- end snippet -->

Please note that I am using .background::before in CSS for the sole purpose of displaying the image as a faded background, for aesthetics.

Now, so far, everything is great. What I want to do is write JavaScript code, which when executed (maybe triggered via a click of a button), it will crop the image to the clipped part only (or the bright part in the image above), and which I should be able to save into a file eventually (in other words, I only want to save the bright section of the image as a new image, or at least be able to display it in a separate img tag)

Thank you.

答案1

得分: 1

After a lot of investigation, and reading the article suggested by @BretDonald, I came up with the following solution. I am putting it here for the greater good.

  1. const cropSize = {
  2. width: 100,
  3. height: 100
  4. }
  5. // Function to crop the image
  6. const crop = () => {
  7. const canvas = document.querySelector("#cropped");
  8. const ctx = canvas.getContext('2d');
  9. ctx.drawImage(img, 70, 140, cropSize.width, cropSize.height, 0, 0, cropSize.width, cropSize.height);
  10. }
  11. const img = new Image();
  12. img.src = 'https://fastly.picsum.photos/id/290/200/300.jpg?hmac=kjRyFwJ6i5kuROjzxcs6QbXbBr8EptbH5AuVxtMxhQ0';
  13. img.onload = (() => {
  14. const canvas = document.querySelector("#image");
  15. const ctx = canvas.getContext('2d');
  16. ctx.drawImage(img, 70, 140, cropSize.width, cropSize.height, 70, 140, cropSize.width, cropSize.height);
  17. });
  1. #main {
  2. display: flex;
  3. flex-direction: column;
  4. align-items: center;
  5. box-sizing: border-box;
  6. width: 500px;
  7. height: 400px;
  8. }
  9. #container {
  10. flex-grow: 1;
  11. box-sizing: border-box;
  12. border: 1px solid black;
  13. background-color: lightgray;
  14. position: relative;
  15. width: 100%;
  16. height: 100%;
  17. display: flex;
  18. flex-direction: column;
  19. justify-content: center;
  20. align-items: center;
  21. }
  22. #image-and-background {
  23. position: relative;
  24. width: 200px;
  25. height: 300px;
  26. background-color: white;
  27. }
  28. #background::before {
  29. content: '';
  30. background: url('https://fastly.picsum.photos/id/290/200/300.jpg?hmac=kjRyFwJ6i5kuROjzxcs6QbXbBr8EptbH5AuVxtMxhQ0') no-repeat;
  31. opacity: 0.3;
  32. position: absolute;
  33. top: 0;
  34. right: 0;
  35. bottom: 0;
  36. left: 0;
  37. }
  1. <div id='main'>
  2. <div id='container'>
  3. <div id='image-and-background'>
  4. <div id='background'>
  5. <canvas id='image' width='200' height='300'></canvas>
  6. </div>
  7. </div>
  8. </div>
  9. </div>
  10. <div>
  11. <p>Click on the "Crop" button below for a cropped image</p>
  12. <canvas id='cropped' width='100' height='100'></canvas>
  13. <div id='buttons'>
  14. <input type="button" id="button1" onclick="crop()" value="Crop" />
  15. </div>
  16. </div>
英文:

After a lot of investigation, and reading the article suggested by @BretDonald, I came up with the following solution. I am putting it here for the greater good.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

  1. const cropSize = {
  2. width: 100,
  3. height: 100
  4. }
  5. // Function to crop the image
  6. const crop = () =&gt; {
  7. const canvas = document.querySelector(&quot;#cropped&quot;);
  8. const ctx = canvas.getContext(&#39;2d&#39;);
  9. ctx.drawImage(img, 70, 140, cropSize.width, cropSize.height, 0, 0, cropSize.width, cropSize.height);
  10. }
  11. const img = new Image();
  12. img.src = &#39;https://fastly.picsum.photos/id/290/200/300.jpg?hmac=kjRyFwJ6i5kuROjzxcs6QbXbBr8EptbH5AuVxtMxhQ0&#39;;
  13. img.onload = (() =&gt; {
  14. const canvas = document.querySelector(&quot;#image&quot;);
  15. const ctx = canvas.getContext(&#39;2d&#39;);
  16. ctx.drawImage(img, 70, 140, cropSize.width, cropSize.height, 70, 140, cropSize.width, cropSize.height);
  17. });

<!-- language: lang-css -->

  1. #main {
  2. display: flex;
  3. flex-direction: column;
  4. align-items: center;
  5. box-sizing: border-box;
  6. width: 500px;
  7. height: 400px;
  8. }
  9. #container {
  10. flex-grow: 1;
  11. box-sizing: border-box;
  12. border: 1px solid black;
  13. background-color: lightgray;
  14. position: relative;
  15. width: 100%;
  16. height: 100%;
  17. display: flex;
  18. flex-direction: column;
  19. justify-content: center;
  20. align-items: center;
  21. }
  22. #image-and-background {
  23. position: relative;
  24. width: 200px;
  25. height: 300px;
  26. background-color: white;
  27. }
  28. #background::before {
  29. content: &#39;&#39;;
  30. background: url(&#39;https://fastly.picsum.photos/id/290/200/300.jpg?hmac=kjRyFwJ6i5kuROjzxcs6QbXbBr8EptbH5AuVxtMxhQ0&#39;) no-repeat;
  31. opacity: 0.3;
  32. position: absolute;
  33. top: 0;
  34. right: 0;
  35. bottom: 0;
  36. left: 0;
  37. }

<!-- language: lang-html -->

  1. &lt;div id=&#39;main&#39;&gt;
  2. &lt;div id=&#39;container&#39;&gt;
  3. &lt;div id=&#39;image-and-background&#39;&gt;
  4. &lt;div id=&#39;background&#39;&gt;
  5. &lt;canvas id=&#39;image&#39; width=&#39;200&#39; height=&#39;300&#39;&gt;&lt;/canvas&gt;
  6. &lt;/div&gt;
  7. &lt;/div&gt;
  8. &lt;/div&gt;
  9. &lt;/div&gt;
  10. &lt;div&gt;
  11. &lt;p&gt;Click on the &quot;Crop&quot; button below for a cropped image&lt;/p&gt;
  12. &lt;canvas id=&#39;cropped&#39; width=&#39;100&#39;, height=&#39;100&#39;&gt;&lt;/canvas&gt;
  13. &lt;div id=&#39;buttons&#39;&gt;
  14. &lt;input type=&quot;button&quot; id=&quot;button1&quot; onclick=&quot;crop()&quot; value=&quot;Crop&quot; /&gt;
  15. &lt;/div&gt;
  16. &lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年6月29日 09:37:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/76577574.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定