此函數,返回一張使用canvas繪制的圖片,返回值符合data:URL格式,格式如下:url = canvas . toDataURL( [ type, 。
])規范規定,在未指定返回圖片類(lèi)型時(shí),返回的圖片格式必須為PNG格式,如果canvas沒(méi)有任何像素,則返回值為:“data:,”,這是最短的data:URL,在text/plain資源中表現為空字符串。type的可以在image/png,image/jpeg,image/svg+xml等 MIME類(lèi)型中選擇。
如果是image/jpeg,可以有第二個(gè)參數,如果第二個(gè)參數的值在0-1之間,則表示JPEG的質(zhì)量等級,否則使用瀏覽器內置默認質(zhì)量等級。下面的代碼可以從ID為codeex的canvas中取得繪制內容,并作為DataURL傳遞給img元素,并顯示。
varcanvas = document.getElementById('codeex');varurl = canvas.toDataURL();//id為myimg的圖片元素myimg.src = url; 1 二維繪圖上下文 當使用一個(gè)canvas元素的getContext(“2d”)方法時(shí),返回的是對象,其內部表現為笛卡爾平面坐標,并且左上角坐標為(0,0),在本平面中往右則x坐標增加和往下方y坐標增加。每一個(gè)canvas元素僅有一個(gè)上下文對象。
其接口如下: {// back-reference to the canvas readonly attribute HTMLCanvasElement canvas;// state void restore(); // pop state stack and restore statevoid save(); // push state on state stack // transformations (default transform is the identity matrix)void rotate(in float angle);void scale(in float x, in float y);void setTransform(in float m11, in float m12, in float m21, in float m22, in float dx, in float dy);void transform(in float m11, in float m12, in float m21, in float m22, in float dx, in float dy);void translate(in float x, in float y);// compositing attribute float globalAlpha; // (default 1.0)attribute DOMString ; // (default source-over)// colors and styles attribute any fillStyle; // (default black)attribute any strokeStyle; // (default black)CanvasGradient createLinearGradient(in float x0, in float y0, in float x1, in float y1);CanvasGradient createRadialGradient(in float x0, in float y0, in float r0, in float x1, in float y1, in float r1);CanvasPattern createPattern(in HTMLImageElement image, in DOMString repetition);CanvasPattern createPattern(in HTMLCanvasElement image, in DOMString repetition);CanvasPattern createPattern(in HTMLVideoElement image, in DOMString repetition);// line styles attribute DOMString lineCap; // butt, round, square (default butt)attribute DOMString lineJoin; // miter, round, bevel (default miter)attribute float lineWidth; // (default 1)attribute float miterLimit; // (default 10)// shadows attribute float shadowBlur; // (default 0)attribute DOMString shadowColor; // (default transparent black)attribute float shadowOffsetX; // (default 0)attribute float shadowOffsetY; // (default 0)// rects void clearRect(in float x, in float y, in float w, in float h);void fillRect(in float x, in float y, in float w, in float h);void strokeRect(in float x, in float y, in float w, in float h);// Complex shapes (paths) API void arc(in float x, in float y, in float radius, in float startAngle, in float endAngle, in boolean anticlockwise);void arcTo(in float x1, in float y1, in float x2, in float y2, in float radius);void beginPath();void bezierCurveTo(in float cp1x, in float cp1y, in float cp2x, in float cp2y, in float x, in float y);void clip();void closePath();void fill();void lineTo(in float x, in float y);void moveTo(in float x, in float y);void quadraticCurveTo(in float cpx, in float cpy, in float x, in float y);void rect(in float x, in float y, in float w, in float h);void stroke();boolean isPointInPath(in float x, in float y);// text attribute DOMString font; // (default 10px sans-serif)attribute DOMString textAlign; // start, end, left, right, center (default: start)attribute DOMString textBaseline; // top, hanging, middle, alphabetic, ideographic, bottom (default: alphabetic)void fillText(in DOMString text, in float x, in float y, optional in float maxWidth);TextMetrics measureText(in DOMString text);void strokeText(in DOMString text, in float x, in float y, optional in float maxWidth);// drawing images void drawImage(in HTMLImageElement image, in float dx, in float dy, optional in float dw, in float dh);void drawImage(in HTMLImageElement image, in float sx, in float sy, in float sw, in float sh, in float dx, in float dy, in float dw, in float dh);void drawImage(in HTMLCanvasElement image, in float dx, in float dy, optional in float dw, in float dh);void drawImage(in HTMLCanvasElement image, in float sx, in float sy, in float sw, in float sh, in float dx, in float dy, in float dw, in float dh);void drawImage(in HTMLVideoElement image, in float dx, in float dy, optional in float dw, in float dh);void drawImage(in HTMLVideoElement image, in float sx, in float sy, in float sw, in float sh, in float dx, in float dy, in float dw, in float dh);// pixel manipulationImageData createImageData(in 。
標簽定義圖形,比如圖表和其他圖像。
這個(gè) HTML 元素是為了客戶(hù)端矢量圖形而設計的。它自己沒(méi)有行為,但卻把一個(gè)繪圖 API 展現給客戶(hù)端 JavaScript 以使腳本能夠把想繪制的東西都繪制到一塊畫(huà)布上。
標記由 Apple 在 Safari 1.3 Web 瀏覽器中引入。對 HTML 的這一根本擴展的原因在于,HTML 在 Safari 中的繪圖能力也為 Mac OS X 桌面的 Dashboard 組件所使用,并且 Apple 希望有一種方式在 Dashboard 中支持腳本化的圖形。
Firefox 1.5 和 Opera 9 都跟隨了 Safari 的引領(lǐng)。這兩個(gè)瀏覽器都支持 標記。
我們甚至可以在 IE 中使用 標記,并在 IE 的 VML 支持的基礎上用開(kāi)源的 JavaScript 代碼(由 Google 發(fā)起)來(lái)構建兼容性的畫(huà)布。參見(jiàn):; charset = utf-8"); this.response = response; out = this.response.getWriter(); println(""); //1. 獲取請求方式、處理亂碼問(wèn)題 String method = request.getMethod(); //servletRequest中的方法 request.setCharacterEncoding("utf-8"); //1. 獲取請求體的編碼方式 String characterEncoding = request.getCharacterEncoding(); println("getCharacterEncoding = " + characterEncoding); //2. get body length int contentLength = request.getContentLength(); println("getContentLength = " + contentLength); //3. MIME type String mimeType = request.getContentType(); println("getContentType = " + mimeType); //4. 接收請求的接口的 Internet Protocol (IP) 地址 String ip = request.getLocalAddr(); println("getLocalAddr = " + ip); //5. 基于 Accept-Language 頭,返回客戶(hù)端將用來(lái)接受內容的首選 Locale 客戶(hù)端語(yǔ)言環(huán)境 Locale locale = request.getLocale(); println("getLocale = " + locale); //6. 所有的語(yǔ)言環(huán)境 Enumeration locales = request.getLocales(); while(locales.hasMoreElements()){ Locale temp = locales.nextElement(); println("\n Locales = " + temp); } //7. 接收請求的 Internet Protocol (IP) 接口的主機名 String localName = request.getLocalName(); println("localName = " + localName); //8. 接收請求的接口的 Internet Protocol (IP) 端口號 int localPort = request.getLocalPort(); println("localPort = " + localPort); //9. 返回請求使用的協(xié)議的名稱(chēng)和版本 String protocol = request.getProtocol(); println("protocol = " + protocol); //10. 讀取請求正文信息 BufferedReader reader = request.getReader(); println("getReader = " + reader.toString()); //11. 發(fā)送請求的客戶(hù)端 String remoteAddr = request.getRemoteAddr(); println("RemoteAddr = " + remoteAddr); //12. 發(fā)送請求的客戶(hù)主機 String remoteHost = request.getRemoteHost(); println("RemoteHost = " + remoteHost); //13. 發(fā)送請求的客戶(hù)主機端口 int remotePort = request.getRemotePort(); println("RemotePort = " + remotePort); //14. 返回用于發(fā)出此請求的方案名稱(chēng),例如:http 、https 、ftp String scheme = request.getScheme(); println("Scheme = " + scheme); //15. 返回請求被發(fā)送到的服務(wù)器的主機名。
它是Host頭值":"(如果有)之前的那部分的值。 或者解析服務(wù)器名稱(chēng)或服務(wù)器的IP地址 String serverName = request.getServerName(); println("ServerName = " + serverName); //16. 返回請求被發(fā)送到的端口。
他是"Host"頭值":" (如果有)之后的那部分的值,或者接受客戶(hù)端連接的服務(wù)器端口。 int serverPort = request.getServerPort(); println("ServerPort = " + serverPort); //17. 返回一個(gè)boolean值,指示此請求是否是使用安全通道(比如HTTPS) 發(fā)出的。
boolean secure = request.isSecure(); println("isSecure = " + secure); //以上方法為 ServletRequest 接口提供的//以下方法為 HttpServletRequest 接口提供的 /* * 18. 返回用于保護servlet的驗證方法名稱(chēng)。 所有的servlet容器都支持 * basic、form和client certificate驗證, 并且可能還支持digest驗證 */ String authType = request.getAuthType(); println("authType = " + authType); //19. getDateHeader ?? request.getDateHeader(""); //20. 返回請求頭包含的所有頭名稱(chēng)的枚舉。
Enumeration headerNames = request.getHeaderNames(); println(""); while(headerNames.hasMoreElements()){ String name = headerNames.nextElement(); println(" headerNmea = " + name + "; getHeader = " + request.getHeader(name)); } println(""); //21. 以int的形式返回指定請求頭的值。 ??? request.getIntHeader("123"); //22. 返回與客戶(hù)端發(fā)出此請求時(shí)發(fā)送的URL相關(guān)聯(lián)的額外路徑信息。
String pathInfo = request.getPathInfo(); println("PathInfo = " + pathInfo); //23. 返回包含在請求RUL中路徑后面的查詢(xún)字符串。如果沒(méi)有查詢(xún)字符串返回null String remoteUser = request.getRemoteUser(); println("RemoteUser = " + remoteUser); //24. 返回客戶(hù)端制定的回話(huà)ID String requestedSessionId = request.getRequestedSessionId(); println("requestSessionId = " + requestedSessionId); //25. 返回請求調用servlet的URL部分 String servletPath = request.getServletPath(); println("servletPath = " + servletPath); //26. 返回與此請求關(guān)聯(lián)的當前HttpSession,如果沒(méi)有當前會(huì )話(huà)并且參數為true,則返回一個(gè)新會(huì )話(huà)。
HttpSession session = request.getSession(true); println("getSession(true) = " + session); //27. 返回包含當前已經(jīng)過(guò)驗證的用戶(hù)的名稱(chēng)的java.security.Principal對象。如果用戶(hù)沒(méi)有經(jīng)過(guò)驗證,則該方法返回null Principal userPrincipal = request.getUserPrincipal(); println("userPrincipal = " + userPrincipal); //28. 檢查會(huì )話(huà)的id是否作為Cook進(jìn)入的 。
主要思想:首先要準備一張有連續幀的圖片,然后利用HTML5Canvas的draw方法在不同的時(shí)間間隔繪制不同的幀,這樣看起來(lái)就像動(dòng)畫(huà)在播放。
關(guān)鍵技術(shù)點(diǎn):JavaScript函數setTimeout()有兩個(gè)參數,第一個(gè)是參數可以傳遞一個(gè)JavaScript方法,另外一個(gè)參數代表間隔時(shí)間,單位為毫秒數。代碼示例:setTimeout(update,1000/30);Canvas的API-drawImage()方法,需要指定全部9個(gè)參數:ctx.drawImage(myImage,offw,offh,width,height,x2,y2,width,height);其中offw,offh是指源圖像的起始坐標點(diǎn),width,height表示源圖像的寬與高,x2,y2表示源圖像在目標Canvas上的起始坐標點(diǎn)。
聲明:本網(wǎng)站尊重并保護知識產(chǎn)權,根據《信息網(wǎng)絡(luò )傳播權保護條例》,如果我們轉載的作品侵犯了您的權利,請在一個(gè)月內通知我們,我們會(huì )及時(shí)刪除。
蜀ICP備2020033479號-4 Copyright ? 2016 學(xué)習?shū)B(niǎo). 頁(yè)面生成時(shí)間:3.030秒