sciter 保存文件到本地

本文介绍了一种使用Sciter框架动态生成CSS背景图像的方法,通过编写脚本实时创建独特的图像,并将其绑定到CSS样式中作为背景。文章详细展示了如何通过随机线条绘制生成图像,并提供了保存图像至本地文件或剪贴板的功能。

<html>
  <head>
    <title>Sciter, Image generators for CSS</title>
    <style>
      html { background: transparent; }
      
      div.test {
        background: url(in-memory:test) stretch;
        size:50%;
        margin:*;
        border:1px solid red;
      }
      
    </style>
    <script type="text/tiscript">

      var dynImage = null;

      function generate()
      {
        function painter(gfx) // generates 1000 random lines on graphics
        {            
          for(var n = 0; n < 1000; ++n)
          {
            var x1 = rand(500);
            var x2 = rand(500);
            var y1 = rand(500);
            var y2 = rand(500);
            gfx.lineWidth(1 + rand(6)); // 1..7
            gfx.lineColor(color(rand(256),rand(256),rand(256)));
            gfx.line(x1,y1,x2,y2);
          }
        }
        var newImage = new Image(500,500,painter);
        // bind image with the URL:
        self.bindImage("in-memory:test", newImage);
        if( dynImage ) dynImage.destroy();
        dynImage = newImage;
        // testing second mode of bindImage(url)
        assert self.bindImage("in-memory:test") instanceof Image;
        
      }      
      
      generate();
      
      event click $(button#test)
      {
        generate();
      }
      
      event click $(button#save-png)
      {
        var bytes = dynImage.toBytes();
        var filter = "PNG Files (*.png)|*.png|All Files (*.*)|*.*";
        var ext = "png";
        var path = view.selectFile(#save, filter, ext);
        if( path )
           bytes.save(path);
      }
      event click $(button#save-jpg)
      {
        var bytes = dynImage.toBytes(85); // 85% quality level
        var filter = "JPEG Files (*.jpg)|*.jpg|All Files (*.*)|*.*";
        var ext = "jpg";
        var path = view.selectFile(#save, filter, ext);
        if( path )
           bytes.save(path);
      }


      event click $(button#to-clipboard)
      {
        view.clipboard(#put, dynImage);
      }
      
      event click $(button#from-clipboard)
      {
        var img = view.clipboard(#get, #picture);
        if( img )
          self.bindImage("in-memory:test", img);
      }
      
    </script>
  </head>
<body>
  <button #test>ReGenerate</button>
  <button #save-png>Save as PNG image to file</button>
  <button #save-jpg>Save as JPEG image to file</button>
  <button #to-clipboard>Save image to clipboard</button>
  <button #from-clipboard>Load image from clipboard</button>
  <div .test>Test</div>
</body>
</html>

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值