學習WebGL:第一個代碼

來源:互聯網
上載者:User

標籤:win   浮點   上下文   支援   tco   console   繪圖   dev   ext   

利用canvas和javascript,就可以在頁面繪製2d映像,但對於3d的映像,目前是無能為力的。要繪製3d映像,就必須使用WebGL。

以我的理解,WebGL是內嵌在瀏覽器中,在使用時是通過javascript呼叫瀏覽器提供的WebGL API,然後在canvas繪圖區繪製映像。剛開始接觸WebGL時,沒有理解這一點,於是覺得代碼怎麼會寫得這麼麻煩,重重複復的寫一堆就為一個簡單的的功能。

下面是一個最簡單的WebGL例子

<!DOCTYPE html><html lang=‘zh-cmn-Hans‘><head><meta charset=‘utf-8‘ /><title>Canvas - WebGL</title></head><body><h1>HTML5 - Canvas - WebGL</h1><canvas id=‘glcanvas‘ width=‘500‘ height=‘500‘></canvas><script type="text/javascript">window.onload = function(){    init();}function init() {    var canvas, gl;    canvas = document.getElementById(‘glcanvas‘);    gl = canvas.getContext(‘webgl‘) || canvas.getContext(‘experimental-webgl‘); //擷取WebGL繪圖上下文    if (!gl) {        console.log(‘不支援WebGL!‘);        return;    }    gl.clearColor(0.0, 0.0, 0.0, 1.0); //設定背景色    gl.clear(gl.COLOR_BUFFER_BIT|gl.DEPTH_BUFFER_BIT); //清空繪圖區}</script></body></html>

 需要注意的是,在WebGL中,顏色RGBA中的值是使用浮點數的,其大小是從0.0到1.0,比如紅色是:(1.0, 0.0, 0.0, 1.0)

學習WebGL:第一個代碼

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.