2017-04-05 1 views
1

캔버스를 사용하여 React에서 카드를 만들고 싶지만 작동하지 않습니다. 질문이 없습니다.canvas.drawImage가 React에서 작동하지 않습니까?

<div> 
    <canvas id="myCanvas" ref="canvas" width="750" height="1000">您的浏览器不支持canvas</canvas> 
    <img src={cardModel1} ref="backImg" style={{display: 'none'}} crossOrigin="anonymous"/> 
    <img ref="showImg" className="showImg" crossOrigin="anonymous" style={showImgStyle}/> 
    <div style={loadingStyleV2}> 
     <LoadingTip /> 
    </div> 
</div> 

자바 스크립트 코드 : 사전에 감사는

코드 반작용

const canvas = this.refs.canvas; 
const ctx = canvas.getContext('2d'); 
let codeUrl = new Image; 
codeUrl.src = this.props.codeUrl; // a http url 
// codeUrl.crossOrigin = "Anonymous"; 
if (codeUrl.complete) { 
    console.log("complete"); 
    ctx.drawImage(codeUrl, 298, 764, 156, 156); 
} else { 
    codeUrl.onload = function (res) { 
    console.log(res,codeUrl); 
    ctx.drawImage(codeUrl, 298, 764, 156, 156); 
}; 
    codeUrl.onerror = function (res) { 
    console.log(res); 
    alert("二维码插入失败!"); 
    } 

을}

jsfiddle : https://jsfiddle.net/HaiSheng/69z2wepo/75587/ 코멘트에, 당신이 할 수있는 무엇에 추가

+1

콘솔의 출력은 무엇입니까? – Kaiido

+0

@Kaiido 유형 : "로드" – wubber

+0

@wubber는 jsfiddle을 만들 수 있습니까? 이렇게하면 코드를 실행할 수 있는지 쉽게 파악할 수 있습니다. – FakeRainBrigand

답변

0

또한이 줄의 주석 처리를 제거하십시오. 캔버스가 크로스 원산지 임에도 불구하고.

codeUrl.crossOrigin = "anonymous"; 

문자열의 대소 문자가 중요한 경우 잘 모르겠습니다.

+0

대소 문자를 구별하지 않지만,''use-credentials'' (i) 및'null' 이외의 다른 값은 익명으로 기본값이됩니다. 그러나 이것은 자신의 서버가 제대로 구성되지 않았다고 말할 수 있으므로 OP의 문제 중 가장 적은 것입니다. 따라서이 속성은 단순히 이미지 로딩을 실패하게 만듭니다. – Kaiido

관련 문제