egret的WebView实现
需求
在egret中嵌入网页,类似
。
网上大概有两种思路吧,一种是直接在body里面加入iframe,如:【Egret】里使用iframe标签达到内嵌多个web界面;另一种就是通过模仿htmlinput来做,如egret的WebView实现。具体要什么样子看需求吧。
Egret由两个层组成,简单看一个页面:

id为“canvasDiv的DIV”层是一个canvas,主要用于文字、位图、矢量图等的渲染,id为“StageDelegateDiv”的层是使用HTML原生的输入文本组件,如下图:

输入文本这块有不少坑,比如焦点啥的
最终代码如下:
/**
* WebView
* 适配FIXED_WIDTH、FIXED_HEIGHT、NO_BORDER、SHOW_ALL四种缩放模式
* 暂未考虑屏幕大小改变、屏幕旋转以及单页面多Webplay实例的情形
* Created by yxiao on 2015/9/30.
*/
class WebView extends egret.DisplayObjectContainer {
private _x:number=0;
private _y:number=0;
private _width:number=0;
private _height:number=0;
private _src:string="";
private _scaleMode:string=egret.MainContext.instance.stage.scaleMode;
private _stageW:number;
private _stageH:number;
private _windowW:number;
private _windowH:number;
private _displayH:number;
private _displayW:number;
private _designH:number;
private _designW:number;
private _iframeWrapper:HTMLDivElement=null;
private _iframe:HTMLIFrameElement=null;
/**
* @param src
*/
public constructor(src:string){
super();
var stageDelegateDom:HTMLElement=document.getElementById("StageDelegateDiv"),playerContainer:HTMLElement=stageDelegateDom.parentElement;
var iframeWrapperDom=document.getElementById("iframe-wrapper");
if(!iframeWrapperDom){
iframeWrapperDom=document.createElement("div");
iframeWrapperDom.style.display="none";
iframeWrapperDom.attributes['style'].value+='position:absolute;-webkit-overflow-scrolling: touch;overflow-y: scroll;';//解决iframe在ios下的显示问题
iframeWrapperDom.id="iframe-wrapper";
stageDelegateDom.appendChild(iframeWrapperDom);
}
this._iframeWrapper=<HTMLDivElement>iframeWrapperDom;
this._iframeWrapper.style.display="none";
this._iframeWrapper.style.opacity="0";
var iframe = document.createElement("iframe"),t=new Date().getTime();
iframe.src=src;
iframe.id="webview-iframe-"+t;
iframe.name="webview-iframe-"+t;
iframe.style.position="absolute";
iframe.style.top="0";
iframe.style.left="0";
iframe.style.opacity="0";
iframe.style.display='none';
iframe.frameBorder='0';
iframe.border="0";
this._iframeWrapper.appendChild(iframe);
this._iframe=<HTMLIFrameElement>document.getElementById("webview-iframe-"+t);
var self=this;
this._iframe.onload=function(){
self._iframeWrapper.style.opacity="1";
self._iframe.style.opacity="1";
}
this._stageW=egret.MainContext.instance.stage.stageWidth;
this._stageH=egret.MainContext.instance.stage.stageHeight;
this._windowW=window.innerWidth;
this._windowH=window.innerHeight;
this._designH=parseInt(playerContainer.attributes['data-content-height'].value);
this._designW=parseInt(playerContainer.attributes['data-content-width'].value);
var stageSize = egret.sys.screenAdapter.calculateStageSize(egret.MainContext.instance.stage.scaleMode, this._windowW, this._windowH, this._designW, this._designH);
this._displayH=stageSize.displayHeight;
this._displayW=stageSize.displayWidth;
console.log("windowW:"+this._windowW);
console.log("stageW:"+this._stageW);
console.log("disPlayW:"+this._displayW);
console.log("windowH:"+this._windowH);
console.log("stageH:"+this._stageH);
console.log("displayH:"+this._displayH);
}
public show():void {
this._iframe.style.display='block';
this._iframeWrapper.style.display='block';
}
public destroy():void {
if(this._iframe){
this._iframeWrapper.style.display="none";
this._iframeWrapper.removeChild(this._iframe);
}
}
public get width():number {
return this._width;
}
public set width(value:number) {
this._width = value;
if(this._scaleMode==egret.StageScaleMode.FIXED_WIDTH || this._scaleMode==egret.StageScaleMode.FIXED_HEIGHT ){
this._iframe.width=this._width/this._stageW*this._windowW+"px";
this._iframeWrapper.style.width=this._width/this._stageW*this._windowW+"px";
}
if(this._scaleMode==egret.StageScaleMode.SHOW_ALL || this._scaleMode==egret.StageScaleMode.NO_BORDER ) {
if(this._windowW==this._displayW){
this._iframe.style.width = this._width / this._stageW * this._windowW + "px";
this._iframeWrapper.style.width = this._width / this._stageW * this._windowW + "px";
}else{
this._iframe.style.width = this._width / this._stageW * this._displayW + "px";
this._iframeWrapper.style.width = this._width / this._stageW * this._displayW + "px";
}
}
}
public get height():number {
return this._height;
}
public set height(value:number) {
this._height = value;
if(this._scaleMode==egret.StageScaleMode.FIXED_WIDTH || this._scaleMode==egret.StageScaleMode.FIXED_HEIGHT ) {
this._iframe.height=this._height/this._stageH*this._windowH+"px";
this._iframeWrapper.style.height=this._height/this._stageH*this._windowH+"px";
}
if(this._scaleMode==egret.StageScaleMode.SHOW_ALL || this._scaleMode==egret.StageScaleMode.NO_BORDER ) {
if(this._windowH==this._displayH){
this._iframe.style.height = this._height / this._stageH * this._windowH + "px";
this._iframeWrapper.style.height = this._height / this._stageH * this._windowH + "px";
}else{
this._iframe.style.height = this._height / this._stageH * this._displayH + "px";
this._iframeWrapper.style.height = this._height / this._stageH * this._displayH + "px";
}
}
}
public set x(value:number) {
this._x = value;
if(this._scaleMode==egret.StageScaleMode.FIXED_WIDTH || this._scaleMode==egret.StageScaleMode.FIXED_HEIGHT) {
this._iframeWrapper.style.left = this._x / this._stageW * this._windowW + "px";
}
if(this._scaleMode==egret.StageScaleMode.SHOW_ALL || this._scaleMode==egret.StageScaleMode.NO_BORDER ) {
if(this._windowW==this._displayW){
this._iframeWrapper.style.left = this._x / this._stageW * this._windowW + "px";
}else{
this._iframeWrapper.style.left = this._x / this._stageW * this._displayW + "px";
}
}
}
public set y(value:number) {
this._y = value;
if(this._scaleMode==egret.StageScaleMode.FIXED_WIDTH || this._scaleMode==egret.StageScaleMode.FIXED_HEIGHT ) {
this._iframeWrapper.style.top = this._y / this._stageH * this._windowH + "px";
}
if(this._scaleMode==egret.StageScaleMode.SHOW_ALL || this._scaleMode==egret.StageScaleMode.NO_BORDER){
if(this._windowH==this._displayH){
this._iframeWrapper.style.top = this._y / this._stageH * this._windowH + "px";
}else{
this._iframeWrapper.style.top =this._y / this._stageH * this._displayH + "px";
}
}
}
public get x():number {
return this._x;
}
public get y():number {
return this._y;
}
public get src():string {
return this._src;
}
public set src(value:string) {
this._src = value;
}
}
参考资料:
http://bbs.egret.com/thread-11245-1-1.html
input输入框的type技巧
http://blog.csdn.net/arvin0/article/details/51437863
【Egret】里使用iframe标签达到内嵌多个web界面
http://blog.csdn.net/xiaoyang0611/article/details/49128077
egret的WebView实现
http://www.html5party.com/2503.html
【HTML5】Egret笔记(一):罗列细碎几个点
egret的WebView实现的更多相关文章
- egret随笔-egret浅入浅出
•不知道有多人跟笔者一样,喜欢学各种技术,但是都不精,但也有一两项算是精的. 自从踏上了egret游戏开发的道路,就不得不学习各种技术了,因为,要精通egret,首先必须要会TypeScript,其次 ...
- Android混合开发之WebView与Javascript交互
前言: 最近公司的App为了加快开发效率选择了一部分功能采用H5开发,从目前市面的大部分App来讲,大致分成Native App.Web App.Hybrid App三种方式,个人觉得目前以Hybri ...
- android通过webview调起支付宝app支付
webview在加载网页的时候会默认调起手机自带的浏览器加载网页,用户体验不好.但当用户设置浏览器客户端(setWebViewClient)设置这样的监听事件之后,当请求url的时候就不会打开手机自带 ...
- [Egret]优雅的写http
首先,自从使用链式调用的写法后,就一发不可收拾的喜爱上了这种优雅的方式.不管是写架构还是写模块,我都会不自觉的使用这种最优雅的方式.链式写法既减少了代码量,又非常优雅的. 在使用 egret 的htt ...
- egret调用页面js的方法。
参考文献: http://bbs.egret-labs.org/thread-267-3-1.html http://docs.egret-labs.org/post/manual/threelibs ...
- egret GUI 和 egret Wing 是我看到h5 最渣的设计
一个抄袭FlexLite抄的连自己思想都没有,别人精髓都不懂的垃圾框架.也不学学MornUI,好歹有点自己想法. 先来个最小可用集合吧: 1. egret create legogame --type ...
- Android WebView 优化页面加载效果
目前带有Web功能的APP越来越多,为了能够更好的使用WebView展示页面,可以考虑做相关的优化:WebView 缓存,资源文件本地存储,客户端UI优化. 可能有些人会说,为什么不做Native的, ...
- Android 浏览器 —— 使用 WebView 实现文件下载
对当前的WebView设置下载监听 mCurrentWebView.setDownloadListener(new DownloadListener() { @Override public void ...
- Android混合开发之WebView使用总结
前言: 今天修改项目中一个有关WebView使用的bug,激起了我总结WebView的动机,今天抽空做个总结. 混合开发相关博客: Android混合开发之WebView使用总结 Android混合开 ...
随机推荐
- Rocket - tilelink - Fragmenter
https://mp.weixin.qq.com/s/kNQrhlf33AErK7IzalnUDw 简单介绍Fragmenter的实现. 1. 基本介绍 用于把上游节点地址空间范 ...
- Rocket - tilelink - FIFOFixer
https://mp.weixin.qq.com/s/JS4Pguwa6LXjPsMq6nW8HA 简单介绍FIFOFixer的实现. 1. 基本介绍 按照一定的策略把某一部分m ...
- 从0开始探究vue-公共变量的管理
背景 在Vue项目中,我们总会遇到一些公共数据的处理,如方法拦截,全局变量等,本文旨在解决这些问题 解决方案 事件总线 所谓事件总线,就是在当前的Vue实例之外,再创建一个Vue实例来专门进行变量传递 ...
- JAVASE(十八) 反射: Class的获取、ClassLoader、反射的应用、动态代理
个人博客网:https://wushaopei.github.io/ (你想要这里多有) 1.反射(JAVA Reflection)的理解 1.1 什么是反射(JAVA Reflection) ...
- Java实现 LeetCode 766 托普利茨矩阵(暴力)
766. 托普利茨矩阵 如果一个矩阵的每一方向由左上到右下的对角线上具有相同元素,那么这个矩阵是托普利茨矩阵. 给定一个 M x N 的矩阵,当且仅当它是托普利茨矩阵时返回 True. 示例 1: 输 ...
- Java实现 蓝桥杯 历届真题 数字拆分
正整数可以表示为若干正整数的累加和. 如,对于正整数n=6,可以分划为: 5+1 4+2 4+1+1 3+3 3+2+1 3+1+1+1 2+2+2 2+2+1+1 2+1+1+1+1 1+1+1+1 ...
- 第九届蓝桥杯JavaB组国(决)赛真题
解题代码部分来自网友,如果有不对的地方,欢迎各位大佬评论 题目1.三角形面积 已知三角形三个顶点在直角坐标系下的坐标分别为: (2.3, 2.5) (6.4, 3.1) (5.1, 7.2) 求该三角 ...
- Python爬虫 爬取搜狗搜索到的内容页面
废话不多说,直接上代码 import requests def main(): url='https://www.sogou.com/web' headers={ 'User_Agent':'Mozi ...
- 涨见识了,在终端执行 Python 代码的 6 种方式!
原作:BRETT CANNON 译者:豌豆花下猫@Python猫 英文:https://snarky.ca/the-many-ways-to-pass-code-to-python-from-the- ...
- 温故知新-java虚拟机
文章目录 java虚拟机是什么? jvm的体系结构 第一个类加载子系统 类的生命周期 加载器分类 类加载机制 第二个运行时数据区(内存结构) GC算法和收集器 如何判断对象可以被回收? 如何判断一个常 ...