无法在画布上获取上下文

时间:2019-06-06 21:14:01

标签: javascript html5-canvas pdfjs

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

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

我可以看到canvas对象是我定义的canvas对象

但是当我打电话给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

3 个答案:

答案 0 :(得分:0)

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

  1. 在JS代码或HTML标记中,该ID错误。
  2. Javascript文件未正确链接到HTML文件

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

希望这会对您有所帮助

答案 1 :(得分:0)

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

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>

答案 2 :(得分:0)

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

对不起..这是漫长的一天

相关问题