Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

174
Views
Setting background image and work on top of it

I am using the below HTML to draw paths using paper.js. On the background we want to set a background image.

I tried to set image 'room.jpeg' which is stored locally. It loads correctly but it is not in the background. As a result it gets removed when I try drawing the path. The image should fit in the browser window.

<script src="https://cdnjs.cloudflare.com/ajax/libs/fabric.js/1.7.14/fabric.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/paper.js/0.12.15/paper-full.js"></script>
<script type="text/paperscript" canvas="canvas">
  var c = document.getElementById("canvas");
  var ctx = c.getContext("2d");
  var img1 = new Image();

  //drawing of the test image - img1
  img1.onload = function() {
    //draw background image
    ctx.drawImage(img1, 0, 0);
    //draw a box over the top
    ctx.fillStyle = "rgba(200, 0, 0, 0.5)";
    ctx.fillRect(0, 0, 500, 500);
  };

  img1.src = 'https://i.imgur.com/6gU9m8O.png'; // 'room.jpeg'; amended for this demo

var path;
  var currentPath = [];

  var textItem = new PointText({
    content: '',
    point: new Point(20, 30),
    fillColor: 'black',
  });

  function onMouseDown(event) {
    // If we produced a path before, deselect it:
    if (path) {
      path.selected = false;
    }

    // Create a new path and set its stroke color to black:
    path = new Path({
      segments: [event.point],
      strokeColor: 'black',
      // Select the path, so we can see its segment points:
      fullySelected: false
    });
  }

  // While the user drags the mouse, points are added to the path
  // at the position of the mouse:
  function onMouseDrag(event) {
    console.log('Capturing new path');
    path.add(event.point);
    var point = event.point;
    currentPath.push(point.x + ', ' + point.y);
    // Update the content of the text item to show how many
    // segments it has:
    textItem.content = '';
  }

  // When the mouse is released, we simplify the path:
  function onMouseUp(event) {
    var segmentCount = path.segments.length;
    // console.log(currentPath.toString());
    console.log('End');
    var poi = prompt("Please enter your poi start and end");
    console.log('Saving Paths' + poi + (currentPath.toString()));
    // When the mouse is released, simplify it:
    path.simplify(10);

    // Select the path, so we can see its segments:
    path.fullySelected = false;

    var newSegmentCount = path.segments.length;
    var difference = segmentCount - newSegmentCount;
    var percentage = 100 - Math.round(newSegmentCount / segmentCount * 100);
    textItem.content = '';
  }
</script>
<canvas id="canvas" resize></canvas>

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Using an img as background gives more trouble since you'll need to redraw/move it on every run.

I'd just use some css to set an background-image to the <canvas> itself:

canvas {
  width: 100vw;
  height: 100vh;
  background-image: url('https://via.placeholder.com/500/FFFFF3?text=BG');
}

canvas {
  width: 100vw;
  height: 100vh;
  background-image: url('https://via.placeholder.com/500/FFFFF3?text=BG');
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/paper.js/0.12.15/paper-full.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/paper.js/0.12.15/paper-core.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/fabric.js/1.7.14/fabric.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
          
<canvas id="canvas" resize></canvas>

<script type="text/paperscript" canvas="canvas">
          
    var c = document.getElementById("canvas");
    var ctx = c.getContext("2d");

    var path;
    var currentPath = [];

    var textItem = new PointText({
        content: '',
        point: new Point(20, 30),
        fillColor: 'black',
    });

    function onMouseDown(event) {
        // If we produced a path before, deselect it:
        if (path) {
            path.selected = false;
        }

        // Create a new path and set its stroke color to black:
        path = new Path({
            segments: [event.point],
            strokeColor: 'black',
            // Select the path, so we can see its segment points:
            fullySelected: false
        });
    }

    // While the user drags the mouse, points are added to the path
    // at the position of the mouse:
    function onMouseDrag(event) {
        //console.log('Capturing new path');
        path.add(event.point);
        var point = event.point;
        currentPath.push(point.x + ', ' + point.y);
        // Update the content of the text item to show how many
        // segments it has:
        textItem.content = '';
    }

    // When the mouse is released, we simplify the path:
    function onMouseUp(event) {
        var segmentCount = path.segments.length;
        // console.log(currentPath.toString());
        // console.log('End');
        var poi = prompt("Please enter your poi start and end");
        console.log('Saving Paths' + poi + (currentPath.toString()));
        // When the mouse is released, simplify it:
        path.simplify(10);

        // Select the path, so we can see its segments:
        path.fullySelected = false;

        var newSegmentCount = path.segments.length;
        var difference = segmentCount - newSegmentCount;
        var percentage = 100 - Math.round(newSegmentCount / segmentCount * 100);
        textItem.content = '';
    }
</script>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!