【问题标题】:Cannot getContext on canvas无法在画布上获取上下文
【发布时间】:2019-06-07 05:41:13
【问题描述】:

调用 canvas.getContext('2d); 会给出一个错误,即画布未定义

var canvas = document.getElementById('sbspdfviewer');
canvas.getContext('2d');

我可以看到画布对象是我定义的画布对象

但是当我打电话给getContext('2d') 我得到画布是未定义的。我看到的所有例子都表明我这样做是正确的..

canvas 标记隐藏在一些 div 标记中.. 但它是页面上唯一的 canvas 标记 这是canvas标签所在的地方

<div id="pdfcontainer" class="collapse collapsed">
   <div  class="panel panel-primary">
     <div class="panel-heading">
        <h3 class="panel-title">Document Review</h3>
    </div>
    <div class="panel-body">
        <div id="pdfviewcontainer" class="row" >
            <div class="col-lg-8 border">
                    <canvas id="sbspdfviewer" style="width: 100%"></canvas>
             </div>
             <div id="codes" class="col-lg-4 border">
                    <div class="padding"></div>
                    <div class="dataTable_wrapper">
                        <table class="table table-striped table-bordered table-hover"
                            id="patientdocumentcodestable" style="width: 100%">
                            <thead>
                                <tr>
                                    <th></th>
                                    <th>Preferred Text</th>
                                    <th>Node</th>
                                </tr>
                            </thead>
                        </table>
                    </div>
            </div>
        </div>
    </div>
</div>

canvas: canvas#sbspdfviewer
accessKey: ""
assignedSlot: null
attributeStyleMap: StylePropertyMap {size: 1}
attributes: NamedNodeMap {0: id, 1: style, id: id, style: style, length: 2}
autocapitalize: ""
baseURI: "http://localhost:8080/invenio/gui/findpatients.jsp"
childElementCount: 0
childNodes: NodeList []
children: HTMLCollection []
classList: DOMTokenList [value: ""]
className: ""
clientHeight: 0
clientLeft: 0
clientTop: 0
clientWidth: 0
contentEditable: "inherit"
dataset: DOMStringMap {}
dir: ""
draggable: false
firstChild: null
firstElementChild: null
height: 150
hidden: false
id: "sbspdfviewer"
innerHTML: ""
innerText: ""
inputMode: ""
isConnected: true
isContentEditable: false
lang: ""
lastChild: null
lastElementChild: null
localName: "canvas"
namespaceURI: "http://www.w3.org/1999/xhtml"
nextElementSibling: null
nextSibling: text
nodeName: "CANVAS"
nodeType: 1
nodeValue: null
nonce: ""
offsetHeight: 0
offsetLeft: 0
offsetParent: null
offsetTop: 0
offsetWidth: 0
onabort: null
onauxclick: null
onbeforecopy: null
onbeforecut: null
onbeforepaste: null
onblur: null
oncancel: null
oncanplay: null
oncanplaythrough: null
onchange: null
onclick: null
onclose: null
oncontextmenu: null
oncopy: null
oncuechange: null
oncut: null
ondblclick: null
ondrag: null
ondragend: null
ondragenter: null
ondragleave: null
ondragover: null
ondragstart: null
ondrop: null
ondurationchange: null
onemptied: null
onended: null
onerror: null
onfocus: null
onfullscreenchange: null
onfullscreenerror: null
ongotpointercapture: null
oninput: null
oninvalid: null
onkeydown: null
onkeypress: null
onkeyup: null
onload: null
onloadeddata: null
onloadedmetadata: null
onloadstart: null
onlostpointercapture: null
onmousedown: null
onmouseenter: null
onmouseleave: null
onmousemove: null
onmouseout: null
onmouseover: null
onmouseup: null
onmousewheel: null
onpaste: null
onpause: null
onplay: null
onplaying: null
onpointercancel: null
onpointerdown: null
onpointerenter: null
onpointerleave: null
onpointermove: null
onpointerout: null
onpointerover: null
onpointerup: null
onprogress: null
onratechange: null
onreset: null
onresize: null
onscroll: null
onsearch: null
onseeked: null
onseeking: null
onselect: null
onselectionchange: null
onselectstart: null
onstalled: null
onsubmit: null
onsuspend: null
ontimeupdate: null
ontoggle: null
onvolumechange: null
onwaiting: null
onwebkitfullscreenchange: null
onwebkitfullscreenerror: null
onwheel: null
outerHTML: "<canvas id="sbspdfviewer" style="width: 100%"></canvas>"
outerText: ""
ownerDocument: document
parentElement: div.col-lg-8.border
parentNode: div.col-lg-8.border
part: DOMTokenList [value: ""]
prefix: null
previousElementSibling: null
previousSibling: text
scrollHeight: 0
scrollLeft: 0
scrollTop: 0
scrollWidth: 0
shadowRoot: null
slot: ""
spellcheck: true
style: CSSStyleDeclaration {0: "width", alignContent: "", alignItems: "", alignSelf: "", alignmentBaseline: "", all: "", …}
tabIndex: -1
tagName: "CANVAS"
textContent: ""
title: ""
translate: true
width: 300
__proto__: HTMLCanvasElement

【问题讨论】:

  • 给我们看。否则我们必须猜测。
  • 比如,你是怎么做到的:我可以看到canvas对象就是我自己定义的canvas对象
  • 查看调试器可以看到元素就是我定义的canvas元素
  • 我们真的需要看看代码,特别是包装canvas.getContext('2d);
  • 您的代码运行良好 codepen.io/anon/pen/XwLpdX 。可能你没有链接js文件,在html渲染之前执行js,或者没有写对canvas id。

标签: javascript html5-canvas pdf.js


【解决方案1】:

您没有提供检查错误的代码,因此我将根据未定义的错误消息提出两点建议:

  1. JS 代码或 HTML 标签中的 id 错误。
  2. Javascript 文件未正确链接到 HTML 文件

检查它们并阅读画布文档并检查示例:https://developer.mozilla.org/en-US/docs/Web/API/Canvas_API/Tutorial/Basic_usage

希望对你有所帮助

【讨论】:

    【解决方案2】:

    对我来说没问题。
    我怀疑您可能会在页面完全加载之前尝试获取画布上下文。
    下面的代码显示了一种等待页面加载后再尝试获取画布的方法。

    document.addEventListener("DOMContentLoaded", function() {
      var canvas = document.getElementById('sbspdfviewer');
      var context = canvas.getContext('2d');
      console.log(context);
    });
    <div id="pdfcontainer" class="collapse collapsed">
       <div  class="panel panel-primary">
         <div class="panel-heading">
            <h3 class="panel-title">Document Review</h3>
        </div>
        <div class="panel-body">
            <div id="pdfviewcontainer" class="row" >
                <div class="col-lg-8 border">
                        <canvas id="sbspdfviewer" style="width: 100%"></canvas>
                 </div>
                 <div id="codes" class="col-lg-4 border">
                        <div class="padding"></div>
                        <div class="dataTable_wrapper">
                            <table class="table table-striped table-bordered table-hover"
                                id="patientdocumentcodestable" style="width: 100%">
                                <thead>
                                    <tr>
                                        <th></th>
                                        <th>Preferred Text</th>
                                        <th>Node</th>
                                    </tr>
                                </thead>
                            </table>
                        </div>
                </div>
            </div>
        </div>
    </div>

    【讨论】:

      【解决方案3】:

      是的,这完全是我自己的错。我误用了 this 关键字。画布被定义为 this.canvas= document.getElementById('myid') 然后我做了 ctx = canvas.getContext('2d')而不是 this.canvas.getContext('2d')。

      对不起……今天辛苦了

      【讨论】:

        猜你喜欢
        • 2011-07-15
        • 1970-01-01
        • 2011-04-22
        • 1970-01-01
        • 2022-08-24
        • 2015-04-29
        • 1970-01-01
        • 2019-12-20
        • 1970-01-01
        相关资源
        最近更新 更多