5

Using a custom cursor in the Butterfly Builder app on this page, when applying the colors via the color palette section, however, Safari is not lining up the cursor where the mouse is moving over the .canvas-container Have tried both of these in order to line this up, but it never works:

var pBucketHTML = $('<div id="pBrush"><span class="color"></span></div>');
pBucketHTML.css('top', ((e.offsetY + $(this).offset().top) - (pBucketHTML.height() / 2)));
pBucketHTML.css('left', ((e.offsetX + $(this).offset().left) - (pBucketHTML.width() * 2)));

AND

var pBucketHTML = $('<div id="pBrush"><span class="color"></span></div>');
pBucketHTML.css('top', e.pageY - (pBucketHTML.height() / 2));
pBucketHTML.css('left', e.pageX - (pBucketHTML.width() * 2));

LESS I'm using for this:

#pBrush {
    width: 24px;
    height: 34px;
    background: transparent url('../images/paintBrush.png') left bottom no-repeat;
    position: fixed;

    .color {
        position: absolute;
        width: 12px;
        height: 12px;
        border: 1px solid #FFFFFF;
        display: inline-block;
        top: 0;
        right: 0;
        left: 0;
        margin: 0 auto;
    }
}

This is loading up the mouse cursor perfectly fine on Chrome and all other browsers, but Safari is not loading it up with the correct X coordinate... not sure why? Looks like the Y coordinate is correct when moving the mouse of the .canvas-container in the Butterfly builder, just not the X coordinate. How to fix this in Safari?

To see what I mean, please visit: http://memorial.garden and click on the "Launch Butterfly Builder" button on the bottom right hand side of the page. Once you get to Step 2, and choose a color palette, you will see the problem with the pBrush element. Not sure why this is happening with the x coordinate being off like that.

Butterfly Builder Custom Custor Problem in Safari

Solomon Closson
  • 6,111
  • 14
  • 73
  • 115
  • 2
    This is weird, I tested in Safari and it works perfectly fine. What version of Safari are you running? – hallleron Sep 01 '17 at 14:48
  • Using Safari - Version 8.0 (10600.1.25.1) on Mac Yosemite if that helps. – Solomon Closson Sep 01 '17 at 18:26
  • 2
    Can confirm it works properly in Safari Version 10.1.2 (11603.3.8) on El Capitan. – Stuart Sep 03 '17 at 10:15
  • Thanks, I think the problem is because of older Safari browser. But don't see option to upgrade Safari on Yosemite. Possibly need to upgrade entire OS first. – Solomon Closson Sep 03 '17 at 20:19
  • Does the same happen when you use `getBoundingClientRect()` instead of `offsetXXX` (or jQuery's `.offset()` which is the same)? – Kaiido Sep 08 '17 at 03:16

2 Answers2

0

I won't go into the details, because it is very well documented here: Position:fixed element within a position:relative parent. Which browser renders correctly?

Basically, the problem is the way that the browsers treat position:fixed; elements.

To solve this issue, use position: relative; in your #pBrush LESS.

#pBrush {
    width: 24px;
    height: 34px;
    background: transparent url('../images/paintBrush.png') left bottom no-repeat;
    position: relative;

    .color {
        position: absolute;
        width: 12px;
        height: 12px;
        border: 1px solid #FFFFFF;
        display: inline-block;
        top: 0;
        right: 0;
        left: 0;
        margin: 0 auto;
    }
}

And in your JavaScript, compensate based on the offset of your canvas

pBucketHTML.css('left', e.pageX - $("#canvasBuilder").offset().left - (pBucketHTML.width() / 2));

You may have to tweak this slightly, but I believe that is what you are looking for.


Edit: In case a hacky work-around for Safari 8 is acceptable, adapted from How to detect my browser version and operating system using JavaScript?:

function returnSafariOffset() {
  var nAgt = navigator.userAgent;

  // We have Safari
  if ((verOffset=nAgt.indexOf("Safari"))!=-1) {
    fullVersion = nAgt.substring(verOffset+7);
    if ((verOffset=nAgt.indexOf("Version"))!=-1) {
      fullVersion = nAgt.substring(verOffset+8);
    }

    majorVersion = parseInt(''+fullVersion,10);
    if (isNaN(majorVersion)) {
      fullVersion  = ''+parseFloat(navigator.appVersion); 
      majorVersion = parseInt(navigator.appVersion,10);
    }

    if (majorVersion == 8) {
      // Return a custom amount for Safari 8:
      return $("#canvasBuilder").offset().left;
    }
  }
  // Return an amount for all other browsers:
  return 0;
}

Then in your JavaScript, you can do something like this:

pBucketHTML.css('left', e.pageX - returnSafariOffset() - (pBucketHTML.width() / 2));

Since you don't seem to be getting the same results as me, you may have to experiment with some form of this to get exactly what you are looking for. Let me know if you have questions.

Jonathan
  • 6,507
  • 5
  • 37
  • 47
  • 1
    This does not fix the problem in Safari unfortunately, and applying this will also cause a problem in all other browsers also, where the offset is now the same as the pic I put up on Safari, this now is the problem in all other browsers. I've tested this on localhost btw. – Solomon Closson Sep 03 '17 at 20:17
  • That's why you need to add the `$("#canvasBuilder").offset().left` – Jonathan Sep 03 '17 at 22:59
  • So it needs to be added? Cause in your example you are subtracting it. I have tried your example, and it causes a problem in all browsers. – Solomon Closson Sep 04 '17 at 06:51
  • I'm sorry. Yes, subtracted like the example. If you do `console.log($("#canvasBuilder").offset().left)` in Chrome console, what does it output? what if you replace it with a fixed number (just for testing purposes) like `pBucketHTML.css('left', e.pageX - 400 - (pBucketHTML.width() / 2));` – Jonathan Sep 04 '17 at 11:04
  • Basically, we are half-way there. Once we have all the browsers responding in the same way, we just have to correct for the same problem in all of them: in this case, to subtract a certain amount from the `pageX`. That distance should be the distance that the canvas (with id `canvasBuilder` on your live site) is from the left side of the page. I'm gone for 4 days without internet, I will reply to any comments then. – Jonathan Sep 04 '17 at 11:50
  • There is no consistency here with all browsers now. Basically, what you provided, will work in Safari, but than will not work in any other browser (displaying an offset in all other browsers where `$('#canvasBuilder').offset().left` is the offset width that it is wrong by). If I use my original question, it works in all browsers, cept Safari version 8 – Solomon Closson Sep 05 '17 at 06:18
  • Sorry - when you said 'all browsers', I assumed you meant all including Safari 8. Here is what I'm getting; let me know which step(s) are NOT correct in your case: 1) Change `position:fixed;` to `position:relative;` for `#pBrush`. 2) Safari 8 still has the problem. 3) All other browser also have the problem. 4) Keeping the new CSS from step 1, subtract `$("#canvasBuilder").offset().left` from `e.pageX`. 5) This fixes the problem in Safari 8, 6) This fixes the problem in all other browsers. – Jonathan Sep 08 '17 at 01:06
  • Yes, this is what I've done. And as I've said, this fixes the problem in Safari 8, but causes an offset in all other browsers. – Solomon Closson Sep 08 '17 at 01:36
  • OK. So if I understand correctly, that means step 6 is where it stops working, is that correct? If so, are you willing to use a hacky workaround for Safari 8? Also, when you get to step 6, is the visible icon in the other browsers too far to the left or right? – Jonathan Sep 08 '17 at 02:50
  • I have updated my answer to include a work-around for Safari 8, in case you are willing to go that route. – Jonathan Sep 08 '17 at 03:17
  • Ok, thanks many for your help with this. I'll experiment with this more and, hopefully, get somewhere with this. – Solomon Closson Sep 08 '17 at 08:41
  • No problem! Just drop another comment if you have any questions and I'll update my answer as needed. – Jonathan Sep 08 '17 at 11:02
0

You should check your JS calculation of your custom pointer first: enter image description here

Blue dot is a real cursor position, red block is your #pBrush. Just make sure #pBrush middle point is the same as your real cursor's position. Then we can think about what's next.

maszynka
  • 196
  • 4
  • 11
  • I originally wanted this offset because clicking on the pBrush does not click through it onto the canvas, and thus doesn't provide a click event on the canvas, perhaps I should've just set `pointer-events: none` on the pBrush element. But I recall this causing another problem, so that's why I didn't do this exactly, and the actual mouse needs to be just outside of the pBrush element. – Solomon Closson Sep 06 '17 at 14:11