rough-stuff / rough

Create graphics with a hand-drawn, sketchy, appearance
http://roughjs.com
MIT License
19.94k stars 617 forks source link

this.canvas.getContext is not a function #15

Closed lleohao closed 6 years ago

lleohao commented 6 years ago
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        #main {
            width: 800px;
            height: 600px;
            margin: 40px auto 0;
            padding: 16px;
            border: 1px solid #ddd;
        }
    </style>
</head>
<body>
<main id="myCanvas"></main>

<script src="../node_modules/roughjs/dist/rough.min.js"></script>
<script>
    window.onload = function () {
        rough.canvas(document.getElementById('myCanvas'));
    }
</script>
</body>
</html>

chrome version: 64.0.3282.186

rough.min.js:1 Uncaught TypeError: this.canvas.getContext is not a function
    at new t (rough.min.js:1)
    at Object.canvas (rough.min.js:1)
    at window.onload (index.html?_ijt=8kj4phaq3k33cqhrm3gobggm4f:25)
lleohao commented 6 years ago

package dependencies

  "dependencies": {
    "roughjs": "^2.0.0"
  }
pshihn commented 6 years ago

your 'document.getElementById('myCanvas')' will return the main object which is not a canvas. You want canvas (https://developer.mozilla.org/en-US/docs/Web/API/Canvas_API)

lleohao commented 6 years ago

Oh, sorry. it is my mistake. Thanks

pshihn commented 6 years ago

Examples may help: https://github.com/pshihn/rough/wiki/Examples