Highlight the coordinates that are located within 20 pixels of the center

I'm currently working on a VueJS project where I have a div acting as a canvas displaying the coordinates of the mouse within that canvas. My goal is to underline the coordinates when they are within 20px of the center, but being new to VueJS, I'm not quite sure how to approach this.

Here's the code:

<div id="canvas" v-bind:class="canvasClasses"  @mousemove="getCoordinates">
    {{coordinates.x}},{{coordinates.y}}
</div>

And here is the corresponding JavaScript:

var canvas = new Vue ({
    el: '#canvas',
    data: {
        canvasClasses: ["canvas", "font"],
        underlineClass: ["underlineFont"],
        coordinates: {
            x: 0,
            y: 0
        } 
    },
    methods: {
        getCoordinates(ml){
            this.coordinates.x = ml.offsetX;
            this.coordinates.y = ml.offsetY;
        }
    }
})

Finally, the CSS:

.canvas{
    width: 500px;
    height: 500px;
    display: block;
    margin-left: auto;
    margin-right: auto;
    padding: 0;
    background-color: #74b9ff;
}

.font{
    font-family: monospace;
    font-size: 1.5rem;
    color: #FFFFFF;
}

.underlineFont{
    text-decoration: underline;
}

Answer №1

Implement a class binding for the coordinates to display the value of underlineClass if they meet the radius requirements:

<span :class="coordsClass">{{coordinates.x}},{{coordinates.y}}</span>

The computed property checks if the current x and y values fall within the specified radius, returning the class name if they do (otherwise no class is returned):

computed: {
  coordsClass() {
    const x = this.coordinates.x;
    const y = this.coordinates.y;
    const xO = this.origin.x;
    const yO = this.origin.y;
    if (x >= xO - this.radius && x <= xO + this.radius
      && y >= yO - this.radius && y <= yO + this.radius) {
      return this.underlineClass; 
    }
  }

Define the radius and origin properties in the data section:

data() {
  return {
    ... // Other data
    origin: {
      x: 0,
      y: 0
    },
    radius: 20 // Radius size in pixels
  }
}

Calculate the center point (origin) of the component when it is mounted:

mounted() {
  this.origin.x = this.$el.clientWidth / 2;
  this.origin.y = this.$el.clientHeight / 2;
}

Check out the demo for reference.

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 incorporate personalized colors into mat-buttons?

Can anyone help me figure out how to use a custom color for a mat-button in my Angular project, instead of just using the default accent or primary colors? <button (click)="gotoDashboardHome()" mat-raised-button color="accent" class= ...

The responseText in AJAX is found to be null following a GET request to a .php file

I'm having trouble retrieving a list of devices from a MySQL database, formatting them into an HTML select/option element in my .php file, and echoing the resulting string of HTML to insert into my main page. However, for some reason, the echoed strin ...

A beginner's guide to efficiently indexing tables in AngularJS

Having Trouble with ng-repeat Indexing <tr ng-repeat="Ward in Wardresult "> <td>{{$index + 1}}</td> ...................... some column ....................... </tr> Although, the output ...

Decrease the width of a div element when its sibling element expands

I am working on a layout where I have two divs placed side by side within a parent div. The left div will have text content, while the right div will contain an image. I want to implement functionality where on button click, the right div can expand or red ...

Enhancing website aesthetics through CSS styling

My goal is to create websites with a fresh, updated design. One example of a site I admire is Manta Media Inc I assume that CSS plays a big role in achieving this look. What keywords should I use when searching on Google to learn how to build a web inte ...

Modify the `div` content based on the selected items from the bootstrap dropdown menu

My navigation bar is built using Bootstrap and contains multiple drop-down items. Now I have another div with the class of col-md-3. Within this div, I would like to display the items of the dropdown menu when hovered over. Currently, hovering over a dro ...

Implementing a redirect following the notification

I'm facing an issue with a function that sends form data to Google Sheets. How can I make the redirect button work after displaying an alert message? I attempted using window.location.href='', but it redirects without submitting the data. & ...

What could be causing the hover effect to not work on the cards in my Svelte component? (HTML+CSS)

Greetings everyone! This is my debut post on this platform, and I'm in urgent need of assistance as I am relatively new to the world of programming. I've been trying tirelessly to implement a hover effect on these cards, but for some reason, it ...

Nestjs crashes with "terminated" on an Amazon EC2 instance

https://i.stack.imgur.com/3GSft.jpgMy application abruptly terminates with the error message "killed" without providing any additional information or stack trace. Interestingly, it functions properly when run locally. Any assistance would be greatly appr ...

Issue with the alignment of a sidebar on a website's html page

Last week, I embarked on the journey to learn HTML and CSS. To put my skills to the test, I decided to recreate an old web page using what I have learned so far. The specific page I am trying to emulate can be found here: However, I am currently facing so ...

Displaying identification when clicked using JavaScript

Hey there, I've been searching for an answer to my issue but haven't had any luck so far. Can anyone assist me? I have a group of links that display specific ids when clicked. It's working fine, but one link should actually trigger the disp ...

Problem with off-screen canvas performance in Firefox

Currently, I am facing an issue that I suspect is due to a mistake on my end, but I can't seem to pinpoint it. (This issue arises in Firefox 8) Here's what I'm doing - I have a large sprite file, from which I extract a single tile and place ...

Having trouble with the nav-item dropdown functionality on Laravel Vue?

Currently utilizing Laravel and Vue.js along with AdminLTE. Initially it was functioning properly, but now... head <!-- Font Awesome Icons --> <link rel="stylesheet" href="plugins/fontawesome-free/css/all.min.css"> <!-- Theme style --> ...

React material-ui responsive carousel is a versatile tool that allows for

I am in the process of creating a music tour website using React material-ui. My goal is to replicate the design of this website: As a newcomer to React, I explored libraries like bootstrap and material-ui before settling on material-ui. However, I'm ...

Issue with Wordpress Submenu Items not Displaying

I've included sub menu items in my primary navigation, but they're not visible at all? I've examined the css and haven't found any code that would hide the sub menu. ul.art-hmenu { display: inline-block; vertical-align: midd ...

Utilizing DOMPDF in conjunction with jQuery to generate PDF files

Apologies for any language errors in my writing. I am attempting to generate a PDF using a portion of HTML, utilizing jQuery, Ajax, and DomPDF. On the server side, I am using the following code: require_once './dompdf/autoload.inc.php'; if(!em ...

Tips for incorporating a set offset while utilizing the scrollTop() function

I have successfully implemented a code that sets a position:fixed to a div when it scrolls past the top of the screen. The code I used is as follows: var $window = $(window), $stickyEl = $('#the-sticky-div'), elTop = $stickyEl.o ...

Distinguishing which async HTTP GET response is providing data within a callback in Node.JS

I've been diving into the NodeSchool async principles tutorials recently. One particular lesson requires: making asynchronous GET requests to 3 URLs collecting data returned in HTTP responses using callbacks printing the collected data from each res ...

Tips for correcting the `/Date(xxxxxxxxxxxxx)/` formatting issue in asp.net mvc

As a programming novice, I am trying to display data from my database server on the web using a datatable in asp.net mvc. After following a tutorial video on YouTube, I encountered an issue where the date and time columns in my table are displaying as /Dat ...

Adjust the element to occupy the entire height

To achieve center alignment on the page, I want to expand an element's margin. The first and third elements should stick to the top and bottom, while the second element has a fixed height and needs to be centered. If my explanation isn't clear e ...