
<!DOCTYPE html><html lang="en"><head> ? ?<meta charset="UTF-8"> ? ?<meta name="viewport" content="width=device-width, initial-scale=1.0"> ? ?<meta http-equiv="X-UA-Compatible" content="ie=edge"> ? ?<title>Document</title> ? ?<!-- <link rel="stylesheet" href="./bootstrap本地文檔/css/bootstrap.css"> ? ?<script src="./bootstrap本地文檔/js/jquery-1.12.4.min.js"></script> ? ?<script src="./bootstrap本地文檔/js/bootstrap.js"></script> --> ? ?<style> ? ?canvas{ ? ? ? ? border: 1px solid #000; ? ? ? ?display: block; ? ? ? ?margin: 50px auto; ? ? ? ?width: 1024px; ? ? ? ?height: 768px; ? ?} ? ?</style></head><body> ? ?<canvas id="canvas"></canvas> ? ?<script> ? ?window.onload = function () { ? ? ? ?var canvas = document.getElementById('canvas') ? ? ? ?var context = canvas.getContext('2d') ? ? ? ?context.width = 1024; ? ? ? ?context.height = 768; ? ? ? ?context.moveTo(0,0); ? ? ? ?context.lineTo(700,700) ? ? ? ?context.stroke() ? ? ? ? ? ?} ? ?</script></body></html>
2019-12-20
寫在style里的樣式影響里,style的width和height有px,window.onload里設(shè)置的寬高有加載順序問題,最好寫在標簽上width='1024' height='768'。個人見解,有不同理解的可以多交流交流。