首页
/ node-x11 技术文档

node-x11 技术文档

2024-12-24 09:17:23作者:温艾琴Wonderful

1. 安装指南

1.1 安装环境要求

  • Node.js:确保已安装 Node.js 环境。
  • npm:Node.js 的包管理工具。

1.2 安装步骤

  1. 打开终端或命令行工具。
  2. 运行以下命令安装 node-x11
    npm install x11
    

1.3 Windows 用户特别说明

Windows 用户需要额外安装 X11 服务器:

  1. 安装 XMingCygwin/X
  2. 获取 node-x11 的副本,可以通过 git 或从 Github 下载。

2. 项目使用说明

2.1 基本使用

node-x11 是一个用于 Node.js 的 X11 协议客户端,支持多种 X11 扩展协议。以下是一个简单的使用示例:

var x11 = require('x11');

var Exposure = x11.eventMask.Exposure;
var PointerMotion = x11.eventMask.PointerMotion;

x11.createClient(function(err, display) {
  if (!err) {
    var X = display.client;
    var root = display.screen[0].root;
    var wid = X.AllocID();
    X.CreateWindow(
      wid,
      root, // new window id, parent
      0,
      0,
      500,
      500, // x, y, w, h
      0,
      0,
      0,
      0, // border, depth, class, visual
      { eventMask: Exposure | PointerMotion } // other parameters
    );
    X.MapWindow(wid);
    var gc = X.AllocID();
    X.CreateGC(gc, wid);
    var white = display.screen[0].white_pixel;
    var black = display.screen[0].black_pixel;
    cidBlack = X.AllocID();
    cidWhite = X.AllocID();
    X.CreateGC(cidBlack, wid, { foreground: black, background: white });
    X.CreateGC(cidWhite, wid, { foreground: white, background: black });
    X.on('event', function(ev) {
      if (ev.type == 12) {
        X.PolyFillRectangle(wid, cidWhite, [0, 0, 500, 500]);
        X.PolyText8(wid, cidBlack, 50, 50, ['Hello, Node.JS!']);
      }
    });
    X.on('error', function(e) {
      console.log(e);
    });
  } else {
    console.log(err);
  }
});

2.2 示例截图

  • 俄罗斯方块游戏
  • XRENDER 渐变效果
  • OpenGL glxgears
  • OpenGL 茶壶

3. 项目API使用文档

3.1 createClient

创建一个 X11 客户端连接。

x11.createClient(function(err, display) {
  // 处理连接成功或失败
});

3.2 CreateWindow

创建一个新窗口。

X.CreateWindow(wid, root, x, y, width, height, border, depth, class, visual, options);

3.3 MapWindow

将窗口映射到屏幕上。

X.MapWindow(wid);

3.4 CreateGC

创建一个图形上下文。

X.CreateGC(gc, wid, options);

3.5 on('event')

监听事件。

X.on('event', function(ev) {
  // 处理事件
});

3.6 on('error')

监听错误。

X.on('error', function(e) {
  // 处理错误
});

4. 项目安装方式

4.1 使用 npm 安装

npm install x11

4.2 手动安装

从 Github 下载项目源码,解压后进入项目目录,运行以下命令:

npm install

通过以上步骤,您可以顺利安装并使用 node-x11 项目。

登录后查看全文
热门项目推荐