添加截图demo

This commit is contained in:
Leo_com
2018-04-10 15:37:55 +08:00
parent 43ec23f976
commit 93bc6a980e
23 changed files with 22157 additions and 0 deletions

67
Screenshot/.gitignore vendored Normal file
View File

@@ -0,0 +1,67 @@
#/////////////////////////////////////////////////////////////////////////////
# Fireball Projects
#/////////////////////////////////////////////////////////////////////////////
library/
temp/
local/
build/
#/////////////////////////////////////////////////////////////////////////////
# Logs and databases
#/////////////////////////////////////////////////////////////////////////////
*.log
*.sql
*.sqlite
#/////////////////////////////////////////////////////////////////////////////
# files for debugger
#/////////////////////////////////////////////////////////////////////////////
*.sln
*.csproj
*.pidb
*.unityproj
*.suo
#/////////////////////////////////////////////////////////////////////////////
# OS generated files
#/////////////////////////////////////////////////////////////////////////////
.DS_Store
ehthumbs.db
Thumbs.db
#/////////////////////////////////////////////////////////////////////////////
# exvim files
#/////////////////////////////////////////////////////////////////////////////
*UnityVS.meta
*.err
*.err.meta
*.exvim
*.exvim.meta
*.vimentry
*.vimentry.meta
*.vimproject
*.vimproject.meta
.vimfiles.*/
.exvim.*/
quick_gen_project_*_autogen.bat
quick_gen_project_*_autogen.bat.meta
quick_gen_project_*_autogen.sh
quick_gen_project_*_autogen.sh.meta
.exvim.app
#/////////////////////////////////////////////////////////////////////////////
# webstorm files
#/////////////////////////////////////////////////////////////////////////////
.idea/
#//////////////////////////
# VS Code
#//////////////////////////
.vscode/

2
Screenshot/README.md Normal file
View File

@@ -0,0 +1,2 @@
# hello-world
Hello world new project template.

View File

@@ -0,0 +1,6 @@
{
"ver": "1.0.1",
"uuid": "29f52784-2fca-467b-92e7-8fd9ef8c57b7",
"isGroup": false,
"subMetas": {}
}

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,7 @@
{
"ver": "1.0.0",
"uuid": "2d2f792f-a40c-49bb-a189-ed176a246e49",
"asyncLoadAssets": false,
"autoReleaseAssets": false,
"subMetas": {}
}

View File

@@ -0,0 +1,6 @@
{
"ver": "1.0.1",
"uuid": "4734c20c-0db8-4eb2-92ea-e692f4d70934",
"isGroup": false,
"subMetas": {}
}

View File

@@ -0,0 +1,56 @@
cc.Class({
extends: cc.Component,
properties: {
label: {
default: null,
type: cc.Label
},
shot: cc.Sprite,
// defaults, set visually when attaching this script to the Canvas
text: 'Hello, World!'
},
// use this for initialization
onLoad: function () {
this.label.string = this.text;
this.screenshot = this.node.addComponent('Screenshot');
},
onBtn_native(event) {
if (cc.sys.isNative ) {
this.screenshot.screenshotNative('test.png', 'low', frame => {
this.shot.spriteFrame = frame;
});
} else {
console.log('is not Native');
}
},
onbtn_web() {
if (cc.sys.isBrowser) {
this.screenshot.screenshotWebGL(frame => {
this.shot.spriteFrame = frame;
});
}else {
console.log('is not isBrowser');
}
},
onbtn_canvas() {
if(cc.sys.isBrowser) {
this.screenshot.screenshotCanvas(frame => {
//切图
let spriteFrame=this.screenshot.cutPicture(frame,cc.rect(300,100,300,300));
this.shot.spriteFrame = spriteFrame;
});
}else {
console.log('is not isBrowser');
}
},
// called every frame
update: function (dt) {
},
});

View File

@@ -0,0 +1,9 @@
{
"ver": "1.0.5",
"uuid": "280c3aec-6492-4a9d-9f51-a9b00b570b4a",
"isPlugin": false,
"loadPluginInWeb": true,
"loadPluginInNative": true,
"loadPluginInEditor": false,
"subMetas": {}
}

View File

@@ -0,0 +1,113 @@
cc.Class({
extends: cc.Component,
properties: {
},
onLoad() { },
/**
* 适用于win android ios
* @param {*} fileName
* @param {*} type
* @param {*} callback
*/
screenshotNative(fileName = 'shot.png', type, callback) {
var size = cc.director.getWinSize();
var fullPath = jsb.fileUtils.getWritablePath() + fileName;
console.log('fullPath=', fullPath);
if (jsb.fileUtils.isFileExist(fullPath)) {
jsb.fileUtils.removeFile(fullPath);
}
//方式1
var texture = new cc.RenderTexture(Math.floor(size.width), Math.floor(size.height));
//方式2
if (type == 'high') {
//如果要图片高质量 可以使用cc.Texture2D.PIXEL_FORMAT_RGBA8888。
texture = new cc.RenderTexture(Math.floor(size.width), Math.floor(size.height), cc.Texture2D.PIXEL_FORMAT_RGBA4444, gl.DEPTH24_STENCIL8_OES);
}
texture.setPosition(cc.p(size.width / 2, size.height / 2));
texture.begin();
cc.director.getRunningScene().visit();
texture.end();
//1.4 之前请用cc.IMAGE_FORMAT_JPG。1.4以后,截屏函数的第二个参数改成了 cc.ImageFormat.PNG
texture.saveToFile(fileName, cc.ImageFormat.PNG);
this.loadImg(fullPath, callback);
},
/**
* 适用于WebGL 与Canvas环境
* @param {*} callback
*/
screenshotWebGL(callback) {
cc.director.on(cc.Director.EVENT_AFTER_DRAW, () => {
var canvas = document.getElementById("GameCanvas");
var base64 = canvas.toDataURL("imagea/png");
cc.director.off(cc.Director.EVENT_AFTER_DRAW);
var frame = this.base64ToSpriteFrame(base64, (frame) => {
if (callback) callback(frame);
});
});
},
/**
* 只适用Canvas环境
* @param {*} callback
*/
screenshotCanvas(callback) {
var canvas = document.getElementById("GameCanvas");
var base64 = canvas.toDataURL("imagea/png");
var frame = this.base64ToSpriteFrame(base64, (frame) => {
if (callback) callback(frame);
});
//把图片生成后download到本地
// var href = base64.replace(/^data:image[^;]*/, "data:image/octet-stream");
// document.location.href = href;
},
/**
* 切图
* @param { cc.SpriteFrame or cc.Texture2D} data
* @param {*} rect
*/
cutPicture(data,rect){
let frame;
if(data instanceof cc.SpriteFrame) {
frame=data;
}else if(data instanceof cc.Texture2D) {
frame = new cc.SpriteFrame(texture);
}
if(!frame) {
return null;
}
//设置显示区域 注意使用cc.Rect()会得到undefinde
frame.setRect(rect);
return frame;
},
base64ToSpriteFrame(base64, callback) {
var img = new Image();
img.src = base64;
img.onload = function () {
var texture = new cc.Texture2D();
texture.initWithElement(img);
texture.handleLoadedTexture();
var newframe = new cc.SpriteFrame(texture);
if (callback) callback(newframe);
}
},
loadImg(fullPath, callback) {
//延时就因为texture.begin()是到是下一帧才截图完成
this.scheduleOnce(() => {
cc.loader.load(fullPath, (err, tex) => {
let spriteFrame = new cc.SpriteFrame(tex, cc.Rect(0, 0, tex.width, tex.height));
if (callback) callback(spriteFrame);
});
}, 0.01);
},
// update (dt) {},
});

View File

@@ -0,0 +1,9 @@
{
"ver": "1.0.5",
"uuid": "d5d834c2-63e6-4862-afd0-6fd75f01c4e0",
"isPlugin": false,
"loadPluginInWeb": true,
"loadPluginInNative": true,
"loadPluginInEditor": false,
"subMetas": {}
}

View File

@@ -0,0 +1,6 @@
{
"ver": "1.0.1",
"uuid": "7b81d4e8-ec84-4716-968d-500ac1d78a54",
"isGroup": false,
"subMetas": {}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 37 KiB

View File

@@ -0,0 +1,30 @@
{
"ver": "1.0.0",
"uuid": "6aa0aa6a-ebee-4155-a088-a687a6aadec4",
"type": "sprite",
"wrapMode": "clamp",
"filterMode": "bilinear",
"subMetas": {
"HelloWorld": {
"ver": "1.0.3",
"uuid": "31bc895a-c003-4566-a9f3-2e54ae1c17dc",
"rawTextureUuid": "6aa0aa6a-ebee-4155-a088-a687a6aadec4",
"trimType": "auto",
"trimThreshold": 1,
"rotated": false,
"offsetX": 0,
"offsetY": 0,
"trimX": 0,
"trimY": 0,
"width": 195,
"height": 270,
"rawWidth": 195,
"rawHeight": 270,
"borderTop": 0,
"borderBottom": 0,
"borderLeft": 0,
"borderRight": 0,
"subMetas": {}
}
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 17 KiB

View File

@@ -0,0 +1,30 @@
{
"ver": "1.0.0",
"uuid": "a8027877-d8d6-4645-97a0-52d4a0123dba",
"type": "sprite",
"wrapMode": "clamp",
"filterMode": "bilinear",
"subMetas": {
"singleColor": {
"ver": "1.0.3",
"uuid": "410fb916-8721-4663-bab8-34397391ace7",
"rawTextureUuid": "a8027877-d8d6-4645-97a0-52d4a0123dba",
"trimType": "auto",
"trimThreshold": 1,
"rotated": false,
"offsetX": 0,
"offsetY": 0,
"trimX": 0,
"trimY": 0,
"width": 2,
"height": 2,
"rawWidth": 2,
"rawHeight": 2,
"borderTop": 0,
"borderBottom": 0,
"borderLeft": 0,
"borderRight": 0,
"subMetas": {}
}
}
}

20520
Screenshot/creator.d.ts vendored Normal file

File diff suppressed because it is too large Load Diff

15
Screenshot/jsconfig.json Normal file
View File

@@ -0,0 +1,15 @@
{
"compilerOptions": {
"target": "es6",
"module": "commonjs",
"experimentalDecorators": true
},
"exclude": [
"node_modules",
".vscode",
"library",
"local",
"settings",
"temp"
]
}

4
Screenshot/project.json Normal file
View File

@@ -0,0 +1,4 @@
{
"engine": "cocos2d-html5",
"packages": "packages"
}

View File

@@ -0,0 +1,33 @@
{
"appKey": "",
"appSecret": "",
"encryptJs": true,
"excludeScenes": [],
"includeAnySDK": false,
"includeSDKBox": false,
"inlineSpriteFrames": true,
"inlineSpriteFrames_native": true,
"jailbreakPlatform": false,
"md5Cache": true,
"mergeStartScene": false,
"oauthLoginServer": "",
"optimizeHotUpdate": false,
"orientation": {
"landscapeLeft": true,
"landscapeRight": true,
"portrait": false,
"upsideDown": false
},
"packageName": "org.cocos2d.helloworld",
"privateKey": "",
"renderMode": "0",
"startScene": "2d2f792f-a40c-49bb-a189-ed176a246e49",
"title": "hello_world",
"webOrientation": "auto",
"wechatgame": {
"appid": "wx6ac3f5090a6b99c5",
"orientation": "landscape"
},
"xxteaKey": "054d16e0-33ee-46",
"zipCompressJs": true
}

View File

@@ -0,0 +1,7 @@
{
"excludeScenes": [],
"packageName": "org.cocos2d.helloworld",
"platform": "web-mobile",
"startScene": "2d2f792f-a40c-49bb-a189-ed176a246e49",
"title": "HelloWorld"
}

View File

@@ -0,0 +1,28 @@
{
"collision-matrix": [
[
true
]
],
"excluded-modules": [],
"group-list": [
"default"
],
"start-scene": "current",
"design-resolution-width": 960,
"design-resolution-height": 640,
"fit-width": false,
"fit-height": true,
"use-project-simulator-setting": false,
"simulator-orientation": false,
"use-customize-simulator": false,
"simulator-resolution": {
"width": 960,
"height": 640
},
"cocos-analytics": {
"enable": false,
"appID": "13798",
"appSecret": "959b3ac0037d0f3c2fdce94f8421a9b2"
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 23 KiB

5
Screenshot/template.json Normal file
View File

@@ -0,0 +1,5 @@
{
"name": "TEMPLATES.helloworld.name",
"desc": "TEMPLATES.helloworld.desc",
"banner": "template-banner.png"
}