位图9宫格 BitmapSlice9.jsfl
/**
* Version 1.1, May 4: fixed issue with symbols in library folders.
**/
/**
* BitmapSlice9 JSFL by Grant Skinner. Apr 13, 2010
* Visit www.gskinner.com/blog for documentation, updates and more free code.
*
*
* Copyright (c) 2010 Grant Skinner
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
**/ var doc = fl.getDocumentDOM();
var minAsVersion = 2;
var minPlayerVersion = 8;
var selection = null;
var layer = null;
var grid = null; run(); function run() {
if (!checkDocument()) { return; }
if (!checkSelection()) { return; }
if (!checkScale9()) { return; } // everything checks out.
slice();
} function checkDocument() {
if (doc == null) {
alert('You must have an FLA open as your active document to run this command.');
return false;
}
if (doc.asVersion < minAsVersion || parseInt(doc.getPlayerVersion()) < minPlayerVersion) {
alert('The Scale9 feature must target Flash Player ' + minPlayerVersion + ' and ActionScript ' + minAsVersion + '.');
return false;
}
return true;
} function checkSelection() {
var selectedItems = doc.selection;
if (selectedItems.length != 1 || selectedItems[0].instanceType != "bitmap") {
alert('Please select a single bitmap object on the stage before running this command.');
return false;
} var selectedFrames = doc.getTimeline().getSelectedFrames();
if (selectedFrames.length != 3) {
alert('Only a single keyframe should be selected before running this command.');
return false;
}
selection = selectedItems[0];
layer = doc.getTimeline().layers[selectedFrames[0]];
frame = layer.frames[selectedFrames[1]];
return true;
} function checkScale9() {
var item = doc.getTimeline().libraryItem; //CS5
if (item == null) {
doc.library.selectItem(doc.getTimeline().name,true,true);
item = doc.library.getSelectedItems()[0];
}
if (item == null) {
alert('The selected bitmap must be in a MovieClip symbol.');
return false;
}
if (!item.scalingGrid) {
if (confirm('Scale 9 must be enabled for the parent symbol. Would you like to enable this now?')) {
item.scalingGrid = true;
}
return false;
}
grid = item.scalingGridRect;
grid = {x:grid.left, y:grid.top, width:grid.right-grid.left, height:grid.bottom-grid.top};
return true;
} function slice() {
var timeline = doc.getTimeline();
var bmp = selection.libraryItem;
var name = bmp.name;
if (name.indexOf(".") > 0) { name = name.substr(0,name.indexOf(".")); }
if (name.indexOf("/") > 0) { name = name.substr(name.lastIndexOf("/")+1); }
var sliceLayer = null;
var bmpLayer = null;
var index; // check if our selection is already on an existing _bmp layer:
if (layer.name.substr(-4) == "_bmp") {
bmpLayer = layer;
name = layer.name.substr(0,layer.name.length-4);
} // check if the slice layer already exists:
var sliceLayerIndexes = timeline.findLayerIndex(name+"_slices");
if (sliceLayerIndexes != null && sliceLayerIndexes.length > 0) {
sliceLayer = timeline.layers[sliceLayerIndexes[0]];
} // create or rename bmpLayer if needed:
if (bmpLayer == null) {
// create a bmpLayer if there are other elements on the current layer.
if (frame.elements.length > 1) {
// create new layer:
doc.clipCut();
if (sliceLayer) { timeline.setSelectedLayers(sliceLayerIndexes[0]); }
index = timeline.addNewLayer(name+"_bmp","guide",true);
bmpLayer = timeline.layers[index];
doc.clipPaste(true);
} else {
// rename the current layer:
layer.name = name+"_bmp";
bmpLayer = layer;
}
} // set up bmpLayer properties:
bmpLayer.visible = false; // hidden
bmpLayer.layerType = "guide"; // avoid compiling it into the SWF if (sliceLayer) {
// sliceLayer already exists, clear old slices:
if (selectSlices(sliceLayer)) { document.deleteSelection(); }
} else {
// create new sliceLayer below the bmpLayer:
index = timeline.addNewLayer(name+"_slices","normal",false);
sliceLayer = timeline.layers[index];
} // ensure the sliceLayer is selected:
timeline.setSelectedLayers(timeline.findLayerIndex(sliceLayer.name)[0]); // find the original size and path of the library bitmap
var bmpWidth = selection.hPixels;
var bmpHeight = selection.vPixels;
var bmpPath = bmp.name; // do the slicing:
var srcRect = {x:selection.x, y:selection.y, width:selection.width, height:selection.height}; var cols = [grid.x-10000, grid.x, grid.x+grid.width, 10000, grid.width, 10000];
var rows = [grid.y-10000, grid.y, grid.y+grid.height, 10000, grid.height, 10000]; for (var row=0; row<3; row++) {
for (var col=0; col<3; col++) {
var targetRect = getIntersection(srcRect, {x:cols[col], y:rows[row], width:cols[col+3], height:rows[row+3]});
drawRect(bmpPath, bmpWidth, bmpHeight, srcRect, targetRect);
}
} selectSlices(sliceLayer);
} function selectSlices(sliceLayer) {
var elements = sliceLayer.frames[0].elements;
var s = [];
for (var i=0; i<elements.length; i++) {
// only remove rectangle primitives, in case there are other items on the layer:
if (elements[i].elementType == "shape" && elements[i].isRectangleObject) {
s.push(elements[i]);
}
}
if (s.length > 0) {
document.selection = s;
}
return s.length > 0;
} function drawRect(bmpPath, bmpWidth, bmpHeight, srcRect, targetRect) {
if (targetRect == null) { return; }
var fill = doc.getCustomFill();
fill.style = "bitmap";
fill.bitmapIsClipped = false;
fill.bitmapPath = bmpPath;
var matrix = selection.matrix;
matrix.tx = srcRect.x;
matrix.ty = srcRect.y;
matrix.a = srcRect.width/(bmpWidth/20); // 20 seems to be a magic number for calculating the matrix.
matrix.d = srcRect.height/(bmpHeight/20);
matrix.b = matrix.c = 0;
fill.matrix = matrix;
doc.setCustomFill(fill);
doc.addNewPrimitiveRectangle({left:targetRect.x, top:targetRect.y, right:targetRect.x+targetRect.width, bottom:targetRect.y+targetRect.height},0,false,true);
} function getIntersection(rect1, rect2) {
var x = max(rect1.x, rect2.x);
var y = max(rect1.y, rect2.y);
var r = min(rect1.x+rect1.width, rect2.x+rect2.width);
var b = min(rect1.y+rect1.height, rect2.y+rect2.height);
if (r > x && b > y) {
return {x:x, y:y, width:r-x, height:b-y}
}
return null;
} function max(num1, num2) {
return (num1 > num2) ? num1 : num2;
}
function min(num1, num2) {
return (num1 < num2) ? num1 : num2;
}
位图9宫格 BitmapSlice9.jsfl的更多相关文章
- 宫格布局实例(注意jquery的版本号要统一)
<!DOCTYPE html><html><head><meta charset="utf-8" /><style> * ...
- 宫格布局实例(注意jquery的版本号要统一)2
<!DOCTYPE html><html><head><meta charset="utf-8" /><style> * ...
- css-九宫格自适应的实现
高度自适应使用padding 或 padding-bottom + 百分比来实现: 宽度自适应使用width + 百分比来实现. 下面是实现九宫格自适应的代码: <!DOCTYPE html&g ...
- HTML5 Canvas中9宫格的坑
近期小鸟情人游戏上了手机qq空间,一个3岁的游戏来了她的第二春.为了能有更好的表现,我们对其进行了一次改版. 改版当中一项就是对原来的弹出框样式进行改进.将大块木板材质改成纯色(边框为圆角金属材质)样 ...
- 微信小程序多宫格抽奖
最近闲来无事,做了一个多宫格抽奖的例子,有什么需要改进或者错误的地方,请留言,谢谢 首先看效果 思路是先让其转动2圈多,然后再进行抽奖,格子运动用的是setTimeout,让其运行的时间间隔不一样,然 ...
- Android自定义多宫格解锁控件
在此之前,一直在想九宫格的实现方法,经过一个上午的初步研究终于完成了一个简单的N*N的宫格解锁组件,代码略显粗糙,仅仅做到简单的实现,界面等后期在做优化,纯粹是学习的目的,在算法上有点缺陷,如果有错误 ...
- vue 如何拿到后台传回的富文本中的img,进行9宫格排列展示以及相关处理
描述: res.data.list 返回的数组, 数组中的每个对象有一个 content,就是传回来的富文本的内容,要拿到这里面的所有的img,进行9宫格排列处理: 1.let img = this. ...
- golang 六宫格、九宫格头像生成
图片示例就不传了,在原WordPress上. //Merge6Grid 6宫格 //rule NO1:至少3张图 最多6张图 // NO2:第一张大小 60*60 其他大小 28*28 间隔4px 合 ...
- Python爬虫学习笔记之微信宫格验证码的识别(存在问题)
本节我们将介绍新浪微博宫格验证码的识别.微博宫格验证码是一种新型交互式验证码,每个宫格之间会有一条 指示连线,指示了应该的滑动轨迹.我们要按照滑动轨迹依次从起始宫格滑动到终止宫格,才可以完成验证,如 ...
随机推荐
- 关于Oracle数据库中SQL空值排序的问题
在Oracle中进行查询排序时,如果排序字段里面有空值的情况下,排序结果可能会达不到自己想要的结果. 如 select * from tableTest order by VISITS desc ...
- [转载]线程间操作无效: 从不是创建控件“ListBox1”的线程访问它
解决方法有两种: 1.Control.CheckForIllegalCrossThreadCalls = false 2.用委托解决线程安全问题
- 团体程序设计天梯赛-练习集L1-008. 求整数段和
L1-008. 求整数段和 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 杨起帆 给定两个整数A和B,输出从A到B的所有整数以及这些 ...
- Bypass Preventing CSRF
CSRF在过去的n年(n>2)一直都火,在bh/defcon/owasp等会议上多次探讨CSRF的攻防[具体你可以看看以往的那些pp].前 段时间PLAYHACK.net上发表了一个总结性的pp ...
- uva 10892
试了一下纯暴力 结果过了 无话可说 应该有更好的方法...... /**************************************************************** ...
- C++中怎么获取类的成员函数的函数指针?
用一个实际代码来说明. class A { public: staticvoid staticmember(){cout<<"static"<<endl;} ...
- overrides final method getUnknownFields.()Lcom/google/protobuf/UnknownFieldSet 错误解决
使用java代码连接hbase服务器报错: java.lang.VerifyError: class org.apache.hadoop.hdfs.protocol.proto.ClientName ...
- C语言itoa()函数和atoi()函数详解(整数转字符)
http://c.biancheng.net/cpp/html/792.html C语言提供了几个标准库函数,可以将任意类型(整型.长整型.浮点型等)的数字转换为字符串. 以下是用itoa()函数将整 ...
- php中对共享内存,消息队列的操作
http://www.cnblogs.com/fengwei/archive/2012/09/12/2682646.html php作为脚本程序,通常生命周期都很短,如在web应用中,一次请求就是ph ...
- 命令行添加用户的“作为服务登录”权利(添加Windows用户的时候,门道不是一般的多)good
1.打开控制台(“开始”|“运行”中输入:MMC) 2.“文件”菜单|“添加删除管理单元”|“添加...”|选“安全模板”|“关闭”. 3.在“C:\Windows\Security\template ...