2

I'm currently using this Code to draw in a canvas with a pen from a Microsoft Surface (on a surface, of course):

<html>
      <head>
        <style>
          /* Disable intrinsic user agent touch behaviors (such as panning or zooming) */
          canvas {
            touch-action: none;
          }
        </style>


      <script type='text/javascript'>
        var lastPt = null;
        var canvas;
        var ctx;

        function init() {
          canvas = document.getElementById("mycanvas");
          ctx = canvas.getContext("2d");
          var offset  = getOffset(canvas);
          if(window.PointerEvent) {
            canvas.addEventListener("pointerdown", function() {
              canvas.addEventListener("pointermove", draw, false);
            }
            , false);
            canvas.addEventListener("pointerup", endPointer, false);
          }
          else {
            //Provide fallback for user agents that do not support Pointer Events
            canvas.addEventListener("mousedown", function() {
              canvas.addEventListener("mousemove", draw, false);
            }
            , false);
            canvas.addEventListener("mouseup", endPointer, false);
          }
        }

        // Event handler called for each pointerdown event:
        function draw(e) {
          if(lastPt!=null) {
            ctx.beginPath();
            // Start at previous point
            ctx.moveTo(lastPt.x, lastPt.y);
            // Line to latest point
            ctx.lineTo(e.pageX, e.pageY);
            // Draw it!
            ctx.stroke();
          }
          //Store latest pointer
          lastPt = {x:e.pageX, y:e.pageY};
        }

        function getOffset(obj) {
          //...
        }

        function endPointer(e) {
          //Stop tracking the pointermove (and mousemove) events
          canvas.removeEventListener("pointermove", draw, false); 
          canvas.removeEventListener("mousemove", draw, false); 

          //Set last point to null to end our pointer path
          lastPt = null;
        }

      </script>
    </head>
    <body onload="init()">
      <canvas id="mycanvas" width="500" height="500" style="border:1px solid black;"></canvas>
    </body>
  </html>

So far so good, its working fine.

What I'm planning to do now, is that the canvas reacts to the pressure on my surface pen.

I know that PointerEvent has a property pressure and I know that for canvas, there is lineWidth. But how can I combine those? So when I only put a little bit of pressure I get a thin line and the more pressure the thicker the line gets?

Thanks

Edit: just realized, that there seems to be a problem in the code, even when drawing with the pen, it seems to jump to the else case (so the fallback for using the mouse), tried to add a console.log into the if part, and its not printed... Why is this?

nameless
  • 1,483
  • 5
  • 32
  • 78
  • So, where are you stuck? Did you try setting the lineWidth based on the `pressure` property of the event? – Cerbrus May 19 '17 at 08:40
  • @Cerbrus currently I realized that the first problem is, that the pointerEvents seem to only work on Internet Explorer so far. Now I moved to IE, now its also in the `if` part, and I can also print out `window.PointerEvent`, but I can't access `pressure`... Also, when I open the `PointerEvent` in the console, for 95% of the attributes (like `pressure`,`rotation`, `pointerID`, etc) I get "permission denied"... – nameless May 19 '17 at 08:53
  • 1
    @Cerbrus okay, little update, when I not access `window.PointerEvent` but `e.PointerEvent` in the `draw` function I can access all atrributes, all are changing (like `x` and `y` values), but `pressure` always is at 0.5.. – nameless May 19 '17 at 09:06
  • @Cerbrus okay, another update, I got it working in IE now, setting `lineWidth = e.pressure`, but in Firefox, I can't even access the object and in Chrome, pressure is always `0,5`... Is this just not fully implemented in other browser then IE? actually I hate IE and it doesn't support other features that I'm using... – nameless May 19 '17 at 09:16
  • Looking at [the documentation](https://developer.mozilla.org/en-US/docs/Web/API/PointerEvent/pressure): _"For hardware that does not support pressure, including but not limited to mouse, the value MUST be 0.5 when the pointer is active and 0 otherwise."_ The property should be available in most browsers. – Cerbrus May 19 '17 at 09:18
  • 1
    @Cerbrus figured out that there is a flag in `about:config` for firefox, now its working in there :) – nameless May 19 '17 at 09:24

0 Answers0