Transforming an image by masking it to create a binocular-like view

I've been experimenting with creating a unique visual effect where an image is revealed through a binocular-shaped element that follows the mouse cursor. The circular part of the element moves along with the cursor, unveiling parts of the image as it moves. I also want to ensure that the mouse cursor stays within the boundaries of the main container.

Here's the image used for this effect:

The surrounding area will remain black, with only the elements visible in the binocular shape as the mouse moves.

Below is the code snippet I've been working with:

  $('.clipping-cursor').on('mousemove', function(e) {

    var parentOffset = $(this).parent().offset();
    var relativeXPosition = (e.pageX - parentOffset.left); //offset -> method allows you to retrieve the current position of an element 'relative' to the document
    var relativeYPosition = (e.pageY - parentOffset.top);

    $('.clipping-cursor').css({
      left: relativeXPosition,
      top: relativeYPosition
    });
  });
.collaborate-wrapper {
  width: 100%;
  height: 90vh;
  background: #000;
  overflow: hidden;
  position: relative;
}
.clipping-cursor {
  width: 1000px;
  height: 580px;
  box-sizing: border-box;
  position: absolute;
  margin-top: -290px;
  margin-left: -500px;
  background-image: url('../images/background/collaborate-2.svg');
  background-repeat: no-repeat;
  background-size: container;
  background-attachment: fixed;
  background-position: center center;
  -webkit-mask-image: url('../images/masking-circle.svg');
  -webkit-mask-size: 100%;
  -webkit-mask-repeat: no-repeat;
  -webkit-mask-position: center;
  border: 1px solid #fff;
  cursor: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="collaborate-wrapper">
  <div class="clipping-cursor">

  </div>
</div>

Answer №1

To achieve this effect, you can utilize SVG in combination with JavaScript to dynamically change position based on the user's mouse movements.

$(".a").mousemove(function(e) {
  var parentOffset = $(this).parent().offset();
  var relX = (e.pageX - parentOffset.left) - 55;
  var relY = (e.pageY - parentOffset.top) - 30;

  $('mask g').attr('transform', 'translate(' + relX + ',' + relY + ')');
});
.a {
  width: 400px;
  height: 200px;
  background: url('http://cdn.images.express.co.uk/img/dynamic/151/590x/secondary/Planet-Nine-443937.jpg');
  background-size: cover;
  background-position: center;
  position: relative;
  display: inline-block;
}
svg {
  position: absolute;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="a">
  <svg x="0px" y="0px" width="400px" height="200px" viewBox="0 0 400 200">
    <mask id="mask">
      <rect width="100%" height="100%" x="0" y="0" fill="white" />
      <g transform="translate(0, 0)">
        <circle cx="30" cy="30" r="30" />
        <circle cx="85" cy="30" r="30" />
      </g>
    </mask>
    <rect x="0" y="0" class="one" mask="url(#mask)" width="400" height="200" />
  </svg>
</div>

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

Issues with Jquery XML functionality in Internet Explorer 8 causing instability

A problem has arisen while using jquery 1.6.2 in IE8. An error message is displayed in the console stating "Unexpected call to method or property access. jquery.min.js, line 17 character 29094." The code functions flawlessly on all modern browsers without ...

"Discover the power of D3.JS with three dazzling charts on a single page, plus a host of additional

As a student, I struggle with English and it makes me feel sorry and anxious... Despite that, I want to create three scatter plot charts on the same page using a single data file (csv). The dataset includes columns for Name, Height, Weight, and Grade (ra ...

Organizing grid elements within the popper component

I am struggling to align the labels of options in the AutoComplete component with their respective column headers in the popper component: https://i.stack.imgur.com/0VMLe.png const CustomPopper = function (props: PopperProps) { co ...

Are you experiencing issues with modal contents extending beyond the modal on smaller screens?

I recently installed a modal plugin called blockUI for my image uploading needs. Although I have styled and positioned everything accordingly, I am facing an issue: Whenever I resize the browser screen, switch to my iPhone, or use another screen, some con ...

Struggling to animate the selector of a nested div in Jquery?

Despite trying numerous variations, I am struggling to identify the correct selector of a nested div. The goal is to animate this particular div, but since no animations are taking place, I suspect that I have not selected the right element. Here is the a ...

Utilizing jQuery-dependent plugins in a Create-React-App: A comprehensive guide

Looking to integrate bootstrap and other jQuery plugins like datepicker and carousel into my React app that is built with create-react-app. This is the method I am using to import jQuery and bootstrap: import React, { Component } from 'react'; ...

Issue with jQuery click event not firing on multiple buttons with identical names

These are the two buttons that appear multiple times but do not function: <button name='btnEditar' class='btn btn-outline-success me-4' data-bs-toggle='modal' data-bs-target='#staticBackdrop'><i class=&a ...

Can I trigger a jQuery event method from within another jQuery event method?

I have created jQuery code that defines a function named "checkPage" to run when the DOM is fully loaded. The purpose of this function is to display the position of the mouse pointer in relation to the document's edge. However, for some reason, the "c ...

Ensure that at least one checkbox is selected by using custom validation in jQuery

My form has checkboxes for gender (male and female) and a set of checkboxes for days of the week (Monday to Sunday). I need to set validation rules so that at least one gender and one day is selected. Below is my jQuery code: $(document).ready(function( ...

How to use jQuery to update a text input field based on a selected <select> dropdown option

Hi there, I could really use some help with this issue... Here's the problem - I need to change the value of a textbox within a table based on the selection of an option in a dropdown. Here's a simplified version of what I mean: <select id= ...

Getting the content inside a div tag with selenium in python

Is there a way to retrieve the content within the specified div tag? I attempted copying and pasting the xpath, but when attempting to print the contents of the div, it did not work. div = driver.find_element_by_xpath('//div[@class="sold_out_tag"]&ap ...

A guide to efficiently deleting multiple items using checkboxes and an Ajax request

Could someone assist me in deleting values with a checkbox using an AJAX call? I'm unable to identify the error. Also, this is a template and it already has a built-in "check all" feature so there's no need to modify any built-in code for the ch ...

Performing multiple service calls in a synchronized way using Ajax

Currently, I am working on a project where I need to make multiple REST service calls to accomplish the required functionality. Furthermore, in order to complete this task, I have to use the response from ServiceCall-ONE as the input for ServiceCall-TWO. S ...

Issues with utilizing Jquery datepicker within a Vue.js component's v-for loop in Laravel

I am facing an issue with the Jquery date picker inside a v-for loop in my vue.js component. The date picker works fine outside of the loop, but inside the loop it does not behave as expected. Here is a snippet of code where the date picker is working cor ...

Seeking guidance on creating an uncomplicated CSS tooltip

Can anyone help me troubleshoot why my CSS tooltip isn't working correctly? I've been trying to create a simple method for tooltips, but they are displaying inconsistently across different browsers. In Firefox, the tooltips appear in random locat ...

Manipulating the DOM by linking to a div and using JSON information for opening and closing

I have retrieved data from a JSON file and displayed it as a link. I am looking to implement functionality that allows me to hide and show a div when the link is clicked. Below is the code I'm currently using: $(document).ready(function() { $ ...

Updating a div using PHP elements

Currently, I'm using a webcam to capture images for a project related to learning. My goal is to showcase the recently taken photos. After taking a photo, it gets stored in a folder. To display all the collected photos within a <div>, I need to ...

What is the best way to update the background color for specific days in fullcalendar?

I have been working on my fullcalendar code and I am trying to figure out how to change the background color only for Feb 14. dayRender: function (date, cell) { var Xmas = new Date('2017-02-14'); var weekday = Xmas ...

Simple method for validating a text box field using jQuery without the need for a form

I am looking for a way to activate jquery unobtrusive validation without a form using the specified pattern: <script type="text/javascript"> $('.btnAddAsync').click(function (e) { e.preventDefault(); // Avoid href p ...

Add an event to your Fullcalendar with a date that is not the same as the present date in your MySQL database

I currently have Fullcalendar set up to display events from a MySQL table (title + datetime) and allow users to add new events by clicking on a specific day within the calendar. However, I am facing an issue where it only adds events to the current day ev ...