【iOS】UIWebView的HTML5扩展之canvas篇
先前公布大那个所谓的"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篇的更多相关文章
- 【微信小程序项目实践总结】30分钟从陌生到熟悉 web app 、native app、hybrid app比较 30分钟ES6从陌生到熟悉 【原创】浅谈内存泄露 HTML5 五子棋 - JS/Canvas 游戏 meta 详解,html5 meta 标签日常设置 C#中回滚TransactionScope的使用方法和原理
[微信小程序项目实践总结]30分钟从陌生到熟悉 前言 我们之前对小程序做了基本学习: 1. 微信小程序开发07-列表页面怎么做 2. 微信小程序开发06-一个业务页面的完成 3. 微信小程序开发05- ...
- iOS开发系列--App扩展开发
概述 从iOS 8 开始Apple引入了扩展(Extension)用于增强系统应用服务和应用之间的交互.它的出现让自定义键盘.系统分享集成等这些依靠系统服务的开发变成了可能.WWDC 2016上众多更 ...
- ios UIWebView截获html并修改便签内容
需求:混合应用UIWebView打开html后,UIWebView有左右滚动条,要去掉左右滚动效果: 方法:通过js截获UIWebView中的html,然后修改html标签内容: 实例代码: 服务器端 ...
- HTML5 界面元素 Canvas 參考手冊
HTML5 界面元素 Canvas 參考手冊 太阳火神的漂亮人生 (http://blog.csdn.net/opengl_es) 本文遵循"署名-非商业用途-保持一致"创作公用协 ...
- iOS冰与火之歌(番外篇) - 基于PEGASUS(Trident三叉戟)的OS X 10.11.6本地提权
iOS冰与火之歌(番外篇) 基于PEGASUS(Trident三叉戟)的OS X 10.11.6本地提权 蒸米@阿里移动安全 0x00 序 这段时间最火的漏洞当属阿联酋的人权活动人士被apt攻击所使用 ...
- HTML5学习总结——canvas绘制象棋(canvas绘图)
一.HTML5学习总结——canvas绘制象棋 1.第一次:canvas绘制象棋(笨方法)示例代码: <!DOCTYPE html> <html> <head> & ...
- HTML5中的canvas基本概念及绘图
* Canvas(画布) * 基本内容 * 简单来说,HTML5提供的新元素<canvas> * Canvas在HTML页面提供画布的功能 * 在画布中绘制各种图形 * Canvas绘制的 ...
- 【javascript】html5中使用canvas编写头像上传截取功能
[javascript]html5中使用canvas编写头像上传截取功能 本人对canvas很是喜欢,于是想仿照新浪微博头像上传功能(前端使用canvas) 本程序目前在谷歌浏览器和火狐浏览器测试可用 ...
- ios UIWebView截获html并修改便签内容(转载)
ios UIWebView截获html并修改便签内容 博客分类: iphone开发iphone开发phoneGap uiwebviewstringByEvaluatingJavaScriptFromS ...
随机推荐
- python学习-- 两种方式查看自己的Django版本
[第一种方式] Windows系统下 按住Windows按键 + R 进入搜索:搜索CMD进入控制台:输入Python进入Python解释器 Linux系统下 直接使用终端调用Python解释器 接下 ...
- 2018"百度之星"程序设计大赛 - 资格赛
调查问卷 Accepts: 1546 Submissions: 6596 Time Limit: 6500/6000 MS (Java/Others) Memory Limit: 262144 ...
- DS博客作业05—树
1.本周学习总结 1.1思维导图 1.2学习体会 本周学习了树的相关知识,了解了树结构体的应用和基本操作 学习了二叉树的遍历,创建以及哈夫曼树的相关操作 通过树的构建等操作熟练了递归的使用 2.PTA ...
- java异常处理及400,404,500错误处理
java代码中经常碰到各种需要处理异常的时候,比如什么IOException SQLException NullPointException等等,在开发web项目中,遇到异常,我现在做的就 ...
- 漫谈登录桩(MockStub)的实现
2014年6月4日,6月的第一个星期三,我正式入职百度,开始baiduer的工作.这不到2个月的时间,因为人力资源这边原因,我从INF部门离开,拉到了百度Hi-Server团队中来.2个完全不着调的岗 ...
- NOJ——1568走走走走走啊走(超级入门DP)
[1568] 走走走走走啊走 时间限制: 1000 ms 内存限制: 65535 K 问题描述 菜菜赚了钱回来,想起要买很多桶回来,不同地方的桶质量是不同的,他在(1,1)点出发因为飞机票有点贵所以他 ...
- 瞄一眼LongAdder(jdk11)
java版本11.0.1,感觉写得太水了,等心情好的时候再重新编辑一下. LongAdder中的核心逻辑主要由java.util.concurrent.atomic.Striped64维护,作为Str ...
- java maven项目testng执行时使用的是test-classes下的文件,共享main下方resource的配置
在pom.xml中配置 <build> <testResources> <testResource> <directory>${project.base ...
- svg优雅降级技术
这是一个名叫Alexey Ten首先提出来的,类似下面的代码: <svg width="96" height="96"> <image xli ...
- best corder MG loves gold
MG loves gold Accepts: 451 Submissions: 1382 Time Limit: 3000/1500 MS (Java/Others) Memory Limit ...