I’m trying remake a javascript code for collision detection in Vue.js Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of I’m trying remake a javascript code for collision detection in Vue.js without wasting too much if your time.

The question is published on by Tutorial Guruji team.

Introduction I am new to Vue.js and I am currently working on my college project which requires collision detection in Vue.js.

Here is the code pen link of the working code: https://codepen.io/dropinks/pen/MrzPXB

I am trying to do the same in vue.js. But I am getting a ReferenceError ‘document is not defined’

Here is a impletmentation what i did:

templete code:

        <div class="container">
            <div class="rectangle-1" id="rect">Hover Me </div>
            <div class="rectangle-2" id="dragMe">Drag Me</div>

script :

export default {
  dragMe: '',

  rect = document.getElementById("rect");
  dragMe = document.getElementById("dragMe");
    element: dragMe,
    drag: function(){isCollapsed(dragMe, rect);},

isCollapsed: function(dragMe, rect){
  var object_1 = dragMe.getBoundingClientRect();
  var object_2 = rect.getBoundingClientRect();
  if (object_1.left < object_2.left + object_2.width  && object_1.left + object_1.width  > object_2.left &&
        object_1.top < object_2.top + object_2.height && object_1.top + object_1.height > object_2.top) {

initDrag: function(options){
  var element = options.element;
  var mousedown, mouseup, mousemove,
      dragStart, initX, initY,
      offsetLeft, offsetTop;
  function mouseMove(ev){
      var newX = offsetLeft + (ev.pageX - initX);
      var newY = offsetTop + (ev.pageY - initY);

      element.style.top = newY+"px";
      element.style.left = newX+"px";
  function mouseUp(ev){
    dragStart = false;
    document.removeEventListener("mousemove", mouseMove, false);
    document.removeEventListener("mouseup", mouseUp, false);
  function mouseDown(ev){
    initX = ev.pageX;
    initY = ev.pageY;
    dragStart = true;
    offsetLeft = element.offsetLeft;
    offsetTop = element.offsetTop;
    document.addEventListener("mousemove", function(ev){mouseMove(ev)}, false);
    document.addEventListener("mouseup", function(ev){mouseUp(ev)}, false);
  element.addEventListener("mousedown", function(ev){mouseDown(ev)}, false);

Here is an image of the error page from nuxt.js

Reference error: document not defined


The error you’re getting is because Nuxt has a server-side context where it’s trying to pre-fetch the data for you and pre-render it.


What you need to do is ensure you’re running that code only on the client-side. I know this sounds confusing, but remember Nuxt is a universal framework, and it tries to render your site on the backend to improve your SEO and yield the initial markup. That’s why you’re getting the error.

What you can do is wrap the code that’s using the document in an if to make sure you’re only running it client-side

  if (process.client) {

The added complexity you’re facing is coming from Nuxt, and not Vue.JS 🙂

EDIT I’ve noticed a lot of mistakes in your question’s code, so if you could please correct them it would be nice! I’ve gone ahead and created an example CodeSanbox with a working solution to your problem which you can checkout. The problem I noticed are:

  • You have syntax error forgetting accessing this.dragMe and this.rect
  • You didn’t return an object in your data ( this is a very important one! )
  • the pasted code isn’t syntaxly correct

Having said all of that here is a working solution from CodeSandbox: https://codesandbox.io/s/zen-panini-n6j8c?file=/pages/index.vue

We are here to answer your question about I’m trying remake a javascript code for collision detection in Vue.js - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji