Preserve the authentic picture along with a blur mask that can be dragged and applied to it

Is there a way to preserve the original image while having a draggable blur mask over it?

If you want to see an example of a draggable blur mask over an image, you can check out this link: https://codepen.io/netsi1964/pen/AXRabW

$(function() {
  $("#mask").draggable({
    containment: "parent"
  });
});
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

#mask {
  width: 50px;
  height: 50px;
  border-radius: 100%;
  overflow: hidden;
  position: absolute;
  top: calc(50% - 25px);
  left: calc(50% - 25px);
}

#unblurred {
  position: absolute;
  top: 0;
  left: 0;
  z-index: 999;
  width: 100%;
  height: 100%;
  overflow: hidden;
  -webkit-filter: blur(0px);
}

#unblurred img {
  position: fixed;
  left: 0;
  top: 0;
}

#blurred {
  -webkit-filter: blur(20px);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js"></script>

<div id="mask">
  <div id="unblurred">
    <img src="https://i.ytimg.com/vi/LRVsxe5OJVY/maxresdefault.jpg">
  </div>
</div>

<img id="blurred" src="https://i.ytimg.com/vi/LRVsxe5OJVY/maxresdefault.jpg">

I'm looking for a way to save images with a draggable blur mask overlay. Maybe using canvas or something similar.

Answer №1

I have managed to come up with a solution that seems to be working. Below is the JavaScript code snippet:

function saveMask() {
    $("#blurred").hide()
    html2canvas(document.querySelector("#mask"), {allowTaint: true}).then(h2c => {
        var pos = $("#mask")[0].getBoundingClientRect();
        $("#mask").hide()
        var image = document.getElementById('blurred');
        var canvas = document.createElement("canvas");
        canvas.height = image.height;
        canvas.width = image.width;

        var ctx = canvas.getContext('2d')
        ctx.filter = 'blur(20px)'
        ctx.drawImage(image, 0, 0);
        ctx.filter = 'none'
        ctx.drawImage(h2c, pos.x, pos.y);

        document.body.appendChild(canvas);
    });
}

The concept behind this solution is to capture the mask using html2canvas and then create a canvas with the blurred image, overlaying the mask on top.

You can see a live example of this functionality here:

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the best way to identify duplicate data being returned from a server within a for loop?

When receiving sorted data from the server through an infinite scroll, my goal is to filter out duplicate entries and add new unique data to a client-side array. If duplicates are found, I want to stop the infinite scrolling process. $scope.collections = ...

Is there a way to adjust the size of the canvas element in HTML without compromising quality or resorting to zooming?

I'm currently working on a basic modeling application for the web. The main component of my app is a canvas element that I'm trying to size correctly. However, when I set the height and width using CSS, it scales the entire canvas and compromises ...

AngularJS function orderBy reverses the array instead of sorting it

I encountered an issue where, after clicking the 'order button', the table does not order as expected. Instead, it reverses all the td elements. For example, 'A', 'C', 'B' becomes 'B', 'C', "A". I ...

Display a custom error message containing a string in an Angular error alert

How can I extract a specific string from an error message? I'm trying to retrieve the phrase "Bad Request" from this particular error message "400 - Bad Request URL: put: Message: Http failure response for : 400 Bad Request Details: "Bad Request ...

Modifying the language of the response (error/success) in stripe.js

I have successfully implemented stripe.js v2 for processing payments in my application catering to a French client. Everything is working smoothly, but I am facing an issue with form validation. I want the error messages to be displayed in French instead o ...

Combining Arrays in AngularJS for ng-repeat Iteration

I store my posts in a .json file, while the likes for each post are saved in a separate php file connected to my database. let likesList = []; $http({ method: 'GET', url: 'data/get_likes.php' }).then(function(response) { ...

Ending or stopping based on data retrieved from an ajax call

Currently, I have a php script that includes an image which, upon clicking, redirects the user to a different page. Additionally, there is an ajax/jQuery function in place to check if the user is logged in or not. When the user clicks on the link, the aja ...

The syntax in JavaScript of (0, fn)(args) is used to call

I came across an interesting piece of JavaScript code in Google's codebase that I wanted to discuss: var myVar = function...; (0, myVar)(args); Do you know what the significance of this syntax is? I'm struggling to see the difference between ( ...

What is the process for integrating a Browserify library module into a project as a standard file?

I have successfully developed a JavaScript library module with browserify --standalone, passing all tests using npm test. Now, I am working on creating a demo application that will utilize this library module in a browser setting. When I run npm update, th ...

Does anyone have any insight on why I can't seem to remove an item from my list of tasks?

Having trouble with my React todo list. After submitting, the list item looks fine. I expect to be able to delete it by clicking on the item, but nothing happens. When I try to add another item, the page refreshes and all items are removed. The console ...

Navigating through a large number of distinct items in Three.JS

I have hit a roadblock in my project development. I am striving to create a level using various phong materials on objects of unique sizes and shapes. However, Three.JS's default texture handling causes them to stretch out and look distorted on meshes ...

Unable to access placeholder information from the controller

I am new to implementing the mean stack. I attempted to view data from the controller, but encountered an error message in the web browser's console. Error: [$controller:ctrlreg] http://errors.angularjs.org/1.6.3/$controller/ctrlreg?p0=AppCtrl Stack ...

Different ways to categorize and tally a collection of objects

I'm having trouble reshaping my data in order to create a stacked bar graph. The data from the backend is structured like this: [ {date: 'January', category: 'A'}, {date: 'January', category: 'B'}, ...

Utilizing jQuery Mobile to tap into smartphone functionalities like camera and calling capabilities

Can mobile features like the camera and sensors be accessed from an HTML page created with jQuery Mobile? In simpler terms, is it feasible to utilize mobile features using jQuery Mobile? It's clear that I will be accessing this page on a mobile phon ...

In order to display the particle-slider logo effect, the JavaScript on the page needs to be refreshed

I have a website frontend integrated from WordPress using an HTML 5 Blank Child Theme. The site features a logo effect utilizing particle slider for screen sizes greater than 960px, and a flat logo image for screen sizes less than 960px. Everything works p ...

Unexpected behavior with async pipe - variable becomes undefined upon reassignment

In my development process, I have implemented 2 components. One is responsible for fetching data from an external service, while the other one displays this data. The constructor of the first component is structured as follows: constructor( private dev ...

Implementing the History API to dynamically load content into a div using Ajax

On my WordPress site, I have post content that loads into a div using AJAX when a user clicks on it. Now, I'd like to update the URL for the current post without using hashes. How can I achieve this with my JavaScript? JavaScript: jQuery(docum ...

What could be the reason for the unexpected invisibility of the 4px margin on a static div?

I have a straightforward situation where I am using a fixed positioning <div> that spans the entire width with 100% width. It is designed to have a margin of 4px on all sides. However, for some reason, the right side margin is not visible. Can someon ...

Using jQuery to implement multiple FadeIn effects

Here is the code that I have written: $('.frame').each(function(){ $(this).click(function(e){ var id = $(this).attr('id'); e.preventDefault(); $('.active').removeClass('active').fadeOut(8 ...

Unable to access JSON data from LocalStorage using jQuery

Currently, I am encountering an issue while attempting to save a JSON array in jQuery to localStorage for future use. Unexpectedly, whenever I make the attempt to save it, an error indicating that the object is not defined arises. Below is my present code: ...