canvas繪製帶有刻度的座標系程式碼例項
分享一段程式碼例項,它實現了繪製帶有刻度的座標系的功能。
程式碼例項如下:
[HTML] 純文字檢視 複製程式碼執行程式碼<!doctype html> <html> <head> <meta charset="utf-8"> <meta name="author" content="http://www.softwhy.com/" /> <title>螞蟻部落</title> </head> <body> <canvas id="canvas" width="500" height="500"></canvas> <script> var canvas = document.getElementById("canvas"), context = canvas.getContext('2d'), AXIS_MARGIN = 40, AXIS_ORIGIN = { x: AXIS_MARGIN, y: canvas.height - AXIS_MARGIN }, AXIS_TOP = AXIS_MARGIN, AXIS_RIGHT = canvas.width - AXIS_MARGIN, HORIZONTAL_TICK_SPACING = 10, VERTICAL_TICK_SPACING = 10, AXIS_WIDTH = AXIS_RIGHT - AXIS_ORIGIN.x, AXIS_HEIGHT = AXIS_ORIGIN.y - AXIS_TOP, NUM_VERTICAL_TICKS = AXIS_HEIGHT / VERTICAL_TICK_SPACING, NUM_HORIZONTAL_TICKS = AXIS_WIDTH / HORIZONTAL_TICK_SPACING, TICK_WIDTH = 10, TICKS_LINEWIDTH = 0.5, TICKS_COLOR = 'navy', AXIS_LINEWIDTH = 1.0, AXIS_COLOR = 'blue'; function drawGrid(color, stepx, stepy) { } function drawAxes() { context.save(); context.strokeStyle = AXIS_COLOR; context.lineWidth = AXIS_LINEWIDTH; drawHorizontalAxis(); drawVerticalAxis(); context.lineWidth = 0.5; context.lineWidth = TICKS_LINEWIDTH; context.strokeStyle = TICKS_COLOR; drawVerticalAxisTicks(); drawHorizontalAxisTicks(); context.restore(); } function drawHorizontalAxis() { context.beginPath(); context.moveTo(AXIS_ORIGIN.x, AXIS_ORIGIN.y); context.lineTo(AXIS_RIGHT, AXIS_ORIGIN.y); context.stroke(); } function drawVerticalAxis() { context.beginPath(); context.moveTo(AXIS_ORIGIN.x, AXIS_ORIGIN.y); context.lineTo(AXIS_ORIGIN.x, AXIS_TOP); context.stroke(); } function drawVerticalAxisTicks() { var deltaX; for (var i = 1; i < NUM_VERTICAL_TICKS; ++i) { context.beginPath(); if (i % 5 === 0) deltaX = TICK_WIDTH; else deltaX = TICK_WIDTH / 2; context.moveTo(AXIS_ORIGIN.x - deltaX, AXIS_ORIGIN.y - i * VERTICAL_TICK_SPACING); context.lineTo(AXIS_ORIGIN.x + deltaX, AXIS_ORIGIN.y - i * VERTICAL_TICK_SPACING); context.stroke(); } } function drawHorizontalAxisTicks() { var deltaY; for (var i = 1; i < NUM_HORIZONTAL_TICKS; ++i) { context.beginPath(); if (i % 5 === 0) deltaY = TICK_WIDTH; else deltaY = TICK_WIDTH / 2; context.moveTo(AXIS_ORIGIN.x + i * HORIZONTAL_TICK_SPACING, AXIS_ORIGIN.y - deltaY); context.lineTo(AXIS_ORIGIN.x + i * HORIZONTAL_TICK_SPACING, AXIS_ORIGIN.y + deltaY); context.stroke(); } } drawGrid('lightgray', 10, 10); drawAxes(); </script> </body> </html>
相關文章
- canvas繪製扇形程式碼例項Canvas
- canvas繪製星星程式碼例項Canvas
- canvas 繪製雞蛋程式碼例項Canvas
- canvas繪製網格程式碼例項Canvas
- canvas繪製笑臉程式碼例項Canvas
- canvas繪製圓環效果程式碼例項Canvas
- canvas繪製米字旗程式碼例項Canvas
- canvas繪製箭頭效果程式碼例項Canvas
- canvas繪製拋物線程式碼例項Canvas線程
- canvas繪製機器貓程式碼例項Canvas
- canvas繪製夜空小屋效果程式碼例項Canvas
- canvas繪製鐘錶效果程式碼例項Canvas
- canvas繪製圓角矩形程式碼例項Canvas
- canvas繪製橢圓效果程式碼例項Canvas
- canvas繪製熊貓頭像程式碼例項Canvas
- canvas繪製魚吃豆效果程式碼例項Canvas
- canvas繪製雪花飄落效果程式碼例項Canvas
- canvas繪製圓形鐘錶程式碼例項Canvas
- canvas繪製實心圓形程式碼例項Canvas
- canvas繪製網狀圓圈程式碼例項Canvas
- canvas繪製機器貓頭像程式碼例項Canvas
- canvas繪製矩形並填充顏色程式碼例項Canvas
- canvas繪製貝濟埃曲線程式碼例項Canvas線程
- canvas繪製憤怒小鳥形象程式碼例項Canvas
- HTML5利用canvas繪製矩形程式碼例項HTMLCanvas
- canvas繪製五角星程式碼例項Canvas
- 如何在 Matlab 中繪製帶箭頭的座標系Matlab
- css繪製圓形程式碼例項CSS
- svg繪製半圓程式碼例項SVG
- canvas繪製帶有漸變效果的直線Canvas
- D3.js座標軸的繪製方法、新增座標軸的刻度和各比例尺的座標軸(V3版本)JS
- SVG拖動繪製矩形程式碼例項SVG
- jQuery繪製網格效果程式碼例項jQuery
- CSS3繪製菱形程式碼例項CSSS3
- canvas刮刮樂程式碼例項Canvas
- canvas座標系原點Canvas
- canvas 座標系原點Canvas
- css3繪製月牙效果程式碼例項CSSS3