先前公布大那个所谓的"HTML5"扩展严格说来还算不是"HTML5"。曲曲几行JS代码就自诩为HTML5扩展多少有些标题党的嫌疑。

而相比之下,本篇的主题canvas能够说算是真正的HTML5扩展了。canvas作为HTML5标准体系下的JavaScript API, 不仅被苹果系统自带的Safari所支持,也被UIWebView类所支持。

以下直接贴上新增类目canvas部分的源码。

完整代码下载地址:https://github.com/duzixi/UIWebView-HTML5 (下载button在页面右下方,“Download ZIP”  . 欢迎fork)

本篇博文首发地址:http://blog.csdn.net/duzixi

<pre name="code" class="objc">@interface UIWebView (Canvas)

///  创建一个指定大小的画布
- (void)createCanvas:(NSString *)canvasId
width:(NSInteger)width
height:(NSInteger)height; /// 在指定位置创建一个指定大小的画布
- (void)createCanvas:(NSString *)canvasId
width:(NSInteger)width
height:(NSInteger)height
x:(NSInteger)x
y:(NSInteger)y; /// 绘制矩形填充 context.fillRect(x,y,width,height)
- (void)fillRectOnCanvas:(NSString *)canvasId
x:(NSInteger)x
y:(NSInteger)y
width:(NSInteger)width
height:(NSInteger)height
uicolor:(UIColor *)color; /// 绘制矩形边框 context.strokeRect(x,y,width,height)
- (void)strokeRectOnCanvas:(NSString *)canvasId
x:(NSInteger)x
y:(NSInteger)y
width:(NSInteger)width
height:(NSInteger)height
uicolor:(UIColor *)color
lineWidth:(NSInteger)lineWidth; /// 清除矩形区域 context.clearRect(x,y,width,height)
- (void)clearRectOnCanvas:(NSString *)canvasId
x:(NSInteger)x
y:(NSInteger)y
width:(NSInteger)width
height:(NSInteger) height; /// 绘制圆弧填充 context.arc(x, y, radius, starAngle,endAngle, anticlockwise)
- (void)arcOnCanvas:(NSString *)canvasId
centerX:(NSInteger)x
centerY:(NSInteger)y
radius:(NSInteger)r
startAngle:(float)startAngle
endAngle:(float)endAngle
anticlockwise:(BOOL)anticlockwise
uicolor:(UIColor *)color; /// 绘制一条线段 context.moveTo(x,y) context.lineTo(x,y)
- (void)lineOnCanvas:(NSString *)canvasId
x1:(NSInteger)x1
y1:(NSInteger)y1
x2:(NSInteger)x2
y2:(NSInteger)y2
uicolor:(UIColor *)color
lineWidth:(NSInteger)lineWidth; /// 绘制一条折线
- (void)linesOnCanvas:(NSString *)canvasId
points:(NSArray *)points
unicolor:(UIColor *)color
lineWidth:(NSInteger)lineWidth; /// 绘制贝塞尔曲线 context.bezierCurveTo(cp1x,cp1y,cp2x,cp2y,x,y)
- (void)bezierCurveOnCanvas:(NSString *)canvasId
x1:(NSInteger)x1
y1:(NSInteger)y1
cp1x:(NSInteger)cp1x
cp1y:(NSInteger)cp1y
cp2x:(NSInteger)cp2x
cp2y:(NSInteger)cp2y
x2:(NSInteger)x2
y2:(NSInteger)y2
unicolor:(UIColor *)color
lineWidth:(NSInteger)lineWidth; /// 绘制二次样条曲线 context.quadraticCurveTo(qcpx,qcpy,qx,qy)
// coming soon... /// 显示图像的一部分 context.drawImage(image,sx,sy,sw,sh,dx,dy,dw,dh)
- (void)drawImage:(NSString *)src
onCanvas:(NSString *)canvasId
sx:(NSInteger)sx
sy:(NSInteger)sy
sw:(NSInteger)sw
sh:(NSInteger)sh
dx:(NSInteger)dx
dy:(NSInteger)dy
dw:(NSInteger)dw
dh:(NSInteger)dh; @end

#pragma mark -
#pragma mark 在网页上绘图 #import "UIColor+Change.h" //ver.2014.7.12 @implementation UIWebView (Canvas) /// 创建一个指定大小的透明画布
- (void)createCanvas:(NSString *)canvasId width:(NSInteger)width height:(NSInteger)height
{
NSString *jsString = [NSString stringWithFormat:
@"var canvas = document.createElement('canvas');"
"canvas.id = %@; canvas.width = %d; canvas.height = %d;"
"document.body.appendChild(canvas);"
"var g = canvas.getContext('2d');"
"g.strokeRect(%d,%d,%d,%d);",
canvasId, width, height, 0 ,0 ,width,height];
[self stringByEvaluatingJavaScriptFromString:jsString];
} /// 在指定位置创建一个指定大小的透明画布
- (void)createCanvas:(NSString *)canvasId width:(NSInteger)width height:(NSInteger)height x:(NSInteger)x y:(NSInteger)y
{
//[self createCanvas:canvasId width:width height:height];
NSString *jsString = [NSString stringWithFormat:
@"var canvas = document.createElement('canvas');"
"canvas.id = %@; canvas.width = %d; canvas.height = %d;"
"canvas.style.position = 'absolute';"
"canvas.style.top = '%d';"
"canvas.style.left = '%d';"
"document.body.appendChild(canvas);"
"var g = canvas.getContext('2d');"
"g.strokeRect(%d,%d,%d,%d);",
canvasId, width, height, y, x, 0 ,0 ,width,height];
[self stringByEvaluatingJavaScriptFromString:jsString];
} /// 绘制矩形填充 context.fillRect(x,y,width,height)
- (void)fillRectOnCanvas:(NSString *)canvasId x:(NSInteger)x y:(NSInteger)y width:(NSInteger)width height:(NSInteger) height uicolor:(UIColor *)color
{ NSString *jsString = [NSString stringWithFormat:
@"var canvas = document.getElementById('%@');"
"var context = canvas.getContext('2d');"
"context.fillStyle = '%@';"
"context.fillRect(%d,%d,%d,%d);"
,canvasId, [color canvasColorString], x, y, width, height];
[self stringByEvaluatingJavaScriptFromString:jsString];
} /// 绘制矩形边框 strokeRect(x,y,width,height)
- (void)strokeRectOnCanvas:(NSString *)canvasId x:(NSInteger)x y:(NSInteger)y width:(NSInteger)width height:(NSInteger) height uicolor:(UIColor *)color lineWidth:(NSInteger)lineWidth
{
NSString *jsString = [NSString stringWithFormat:
@"var canvas = document.getElementById('%@');"
"var context = canvas.getContext('2d');"
"context.strokeStyle = '%@';"
"context.lineWidth = '%d';"
"context.strokeRect(%d,%d,%d,%d);"
,canvasId, [color canvasColorString], lineWidth, x, y, width, height];
[self stringByEvaluatingJavaScriptFromString:jsString];
} /// 清除矩形区域 context.clearRect(x,y,width,height)
- (void)clearRectOnCanvas:(NSString *)canvasId x:(NSInteger)x y:(NSInteger)y width:(NSInteger)width height:(NSInteger) height
{
NSString *jsString = [NSString stringWithFormat:
@"var canvas = document.getElementById('%@');"
"var context = canvas.getContext('2d');"
"context.clearRect(%d,%d,%d,%d);"
,canvasId, x, y, width, height];
[self stringByEvaluatingJavaScriptFromString:jsString];
} /// 绘制圆弧填充 context.arc(x, y, radius, starAngle,endAngle, anticlockwise)
- (void)arcOnCanvas:(NSString *)canvasId centerX:(NSInteger)x centerY:(NSInteger)y radius:(NSInteger)r startAngle:(float)startAngle endAngle:(float)endAngle anticlockwise:(BOOL)anticlockwise uicolor:(UIColor *)color
{
NSString *jsString = [NSString stringWithFormat:
@"var canvas = document.getElementById('%@');"
"var context = canvas.getContext('2d');"
"context.beginPath();"
"context.arc(%d,%d,%d,%f,%f,%@);"
"context.closePath();"
"context.fillStyle = '%@';"
"context.fill();",
canvasId, x, y, r, startAngle, endAngle, anticlockwise ? @"true" : @"false", [color canvasColorString]];
[self stringByEvaluatingJavaScriptFromString:jsString];
} /// 绘制一条线段 context.moveTo(x,y) context.lineTo(x,y)
- (void)lineOnCanvas:(NSString *)canvasId x1:(NSInteger)x1 y1:(NSInteger)y1 x2:(NSInteger)x2 y2:(NSInteger)y2 uicolor:(UIColor *)color lineWidth:(NSInteger)lineWidth
{
NSString *jsString = [NSString stringWithFormat:
@"var canvas = document.getElementById('%@');"
"var context = canvas.getContext('2d');"
"context.beginPath();"
"context.moveTo(%d,%d);"
"context.lineTo(%d,%d);"
"context.closePath();"
"context.strokeStyle = '%@';"
"context.lineWidth = %d;"
"context.stroke();",
canvasId, x1, y1, x2, y2, [color canvasColorString], lineWidth];
[self stringByEvaluatingJavaScriptFromString:jsString];
} /// 绘制一条折线
- (void)linesOnCanvas:(NSString *)canvasId points:(NSArray *)points unicolor:(UIColor *)color lineWidth:(NSInteger)lineWidth
{
NSString *jsString = [NSString stringWithFormat:
@"var canvas = document.getElementById('%@');"
"var context = canvas.getContext('2d');"
"context.beginPath();",
canvasId]; for (int i = 0; i < [points count] / 2; i++) {
jsString = [jsString stringByAppendingFormat:@"context.lineTo(%@,%@);",
points[i * 2], points[i * 2 + 1]];
} jsString = [jsString stringByAppendingFormat:@""
"context.strokeStyle = '%@';"
"context.lineWidth = %d;"
"context.stroke();",
[color canvasColorString], lineWidth];
[self stringByEvaluatingJavaScriptFromString:jsString];
} /// 绘制贝塞尔曲线 context.bezierCurveTo(cp1x,cp1y,cp2x,cp2y,x,y)
- (void)bezierCurveOnCanvas:(NSString *)canvasId
x1:(NSInteger)x1
y1:(NSInteger)y1
cp1x:(NSInteger)cp1x
cp1y:(NSInteger)cp1y
cp2x:(NSInteger)cp2x
cp2y:(NSInteger)cp2y
x2:(NSInteger)x2
y2:(NSInteger)y2
unicolor:(UIColor *)color
lineWidth:(NSInteger)lineWidth
{
NSString *jsString = [NSString stringWithFormat:
@"var canvas = document.getElementById('%@');"
"var context = canvas.getContext('2d');"
"context.beginPath();"
"context.moveTo(%d,%d);"
"context.bezierCurveTo(%d,%d,%d,%d,%d,%d);"
"context.strokeStyle = '%@';"
"context.lineWidth = %d;"
"context.stroke();",
canvasId, x1, y1, cp1x, cp1y, cp2x, cp2y, x2, y2, [color canvasColorString], lineWidth];
[self stringByEvaluatingJavaScriptFromString:jsString];
} /// 显示图像的一部分 context.drawImage(image,sx,sy,sw,sh,dx,dy,dw,dh)
- (void)drawImage:(NSString *)src
onCanvas:(NSString *)canvasId
sx:(NSInteger)sx
sy:(NSInteger)sy
sw:(NSInteger)sw
sh:(NSInteger)sh
dx:(NSInteger)dx
dy:(NSInteger)dy
dw:(NSInteger)dw
dh:(NSInteger)dh
{
NSString *jsString = [NSString stringWithFormat:
@"var image = new Image();"
"image.src = '%@';"
"var canvas = document.getElementById('%@');"
"var context = canvas.getContext('2d');"
"context.drawImage(image,%d,%d,%d,%d,%d,%d,%d,%d)",
src, canvasId, sx, sy, sw, sh, dx, dy, dw, dh];
[self stringByEvaluatingJavaScriptFromString:jsString];
} @end

熟知canvas的朋友都知道,它的功能绝不不过上面列出的这些。因为近期工作比較繁忙,先把这些比較主要的献给大家。

尤其是最后一个方法,是我的一个学生做项目时问起的。问是否有方法能截取网页上的图片。让它显示一部分。

相信也有很多朋友有类似的需求。代码匆忙整理。简单測试,若有疏忽。欢迎指正。

【iOS】UIWebView的HTML5扩展之canvas篇的更多相关文章

  1. 【微信小程序项目实践总结】30分钟从陌生到熟悉 web app 、native app、hybrid app比较 30分钟ES6从陌生到熟悉 【原创】浅谈内存泄露 HTML5 五子棋 - JS/Canvas 游戏 meta 详解,html5 meta 标签日常设置 C#中回滚TransactionScope的使用方法和原理

    [微信小程序项目实践总结]30分钟从陌生到熟悉 前言 我们之前对小程序做了基本学习: 1. 微信小程序开发07-列表页面怎么做 2. 微信小程序开发06-一个业务页面的完成 3. 微信小程序开发05- ...

  2. iOS开发系列--App扩展开发

    概述 从iOS 8 开始Apple引入了扩展(Extension)用于增强系统应用服务和应用之间的交互.它的出现让自定义键盘.系统分享集成等这些依靠系统服务的开发变成了可能.WWDC 2016上众多更 ...

  3. ios UIWebView截获html并修改便签内容

    需求:混合应用UIWebView打开html后,UIWebView有左右滚动条,要去掉左右滚动效果: 方法:通过js截获UIWebView中的html,然后修改html标签内容: 实例代码: 服务器端 ...

  4. HTML5 界面元素 Canvas 參考手冊

    HTML5 界面元素 Canvas 參考手冊 太阳火神的漂亮人生 (http://blog.csdn.net/opengl_es) 本文遵循"署名-非商业用途-保持一致"创作公用协 ...

  5. iOS冰与火之歌(番外篇) - 基于PEGASUS(Trident三叉戟)的OS X 10.11.6本地提权

    iOS冰与火之歌(番外篇) 基于PEGASUS(Trident三叉戟)的OS X 10.11.6本地提权 蒸米@阿里移动安全 0x00 序 这段时间最火的漏洞当属阿联酋的人权活动人士被apt攻击所使用 ...

  6. HTML5学习总结——canvas绘制象棋(canvas绘图)

    一.HTML5学习总结——canvas绘制象棋 1.第一次:canvas绘制象棋(笨方法)示例代码: <!DOCTYPE html> <html> <head> & ...

  7. HTML5中的canvas基本概念及绘图

    * Canvas(画布) * 基本内容 * 简单来说,HTML5提供的新元素<canvas> * Canvas在HTML页面提供画布的功能 * 在画布中绘制各种图形 * Canvas绘制的 ...

  8. 【javascript】html5中使用canvas编写头像上传截取功能

    [javascript]html5中使用canvas编写头像上传截取功能 本人对canvas很是喜欢,于是想仿照新浪微博头像上传功能(前端使用canvas) 本程序目前在谷歌浏览器和火狐浏览器测试可用 ...

  9. ios UIWebView截获html并修改便签内容(转载)

    ios UIWebView截获html并修改便签内容 博客分类: iphone开发iphone开发phoneGap uiwebviewstringByEvaluatingJavaScriptFromS ...

随机推荐

  1. javascript 获取键盘上的按键代码KeyCode

    Enter键的keyCode为13 Alt + Enter 的keyCode为10 $(document).on( 'keypress', function ( e ) { console.log( ...

  2. Python socket粘包问题(初级解决办法)

    server端配置: import socket,subprocess,struct from socket import * server=socket(AF_INET,SOCK_STREAM) s ...

  3. [error:没有解析库]Couldn't find a tree builder with the features you requested: xml. Do you need to install a parser library?

    将代码拷贝到服务器上运行,发生错误提示需要新安装parser library. 查看代码中发现有以下内容: soup = BeautifulSoup(open(fp), 'xml') 安装解析库即可: ...

  4. 【Luogu】P3396哈希冲突(根号算法)

    题目链接 根号算法真的是博大精深啊……明明是暴力但复杂度就是能过 这也太强了吧!!! 预处理出p<=sqrt(n)的所有情况,耗时n根n 查询: 如果p<=根n,O1查表 如果p>= ...

  5. 二进制<2>

    位运算简介及实用技巧(二):进阶篇(1) =====   真正强的东西来了!   ===== 二进制中的1有奇数个还是偶数个    我们可以用下面的代码来计算一个32位整数的二进制中1的个数的奇偶性, ...

  6. NOJ——1508火烧赤壁2(并查集+启发式合并+逆序加边)

    [1508] 火烧赤壁2 时间限制: 1000 ms 内存限制: 65535 K 问题描述 上次出了一道火烧赤壁的题目给当时的新生,也就是你们的上一届学长们做,那么这次,我又想到了另一个想法. 上次的 ...

  7. 刷题总结——Tree chain problem(HDU 5293 树形dp+dfs序+树状数组)

    题目: Problem Description Coco has a tree, whose vertices are conveniently labeled by 1,2,…,n.There ar ...

  8. 2017.8.2 Noip2018模拟测试赛(十八)

     日期: 八月二日  总分: 300分  难度: 提高 ~ 省选  得分: 40分(又炸蛋了!!) 题目列表: T1:分手是祝愿 T2:残缺的字符串 T3:树点涂色 赛后心得: 哎,T1求期望,放弃. ...

  9. 标准C程序设计七---74

    Linux应用             编程深入            语言编程 标准C程序设计七---经典C11程序设计    以下内容为阅读:    <标准C程序设计>(第7版) 作者 ...

  10. hdu 5690(模运算)

    All X Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submi ...